ENHANCEMENT: using symfony framework

main
Jeroen De Meerleer 4 months ago
parent 75ece7ea27
commit 0b6d123b69
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
  1. 8
      .env.sample
  2. 29
      .gitignore
  3. 12
      assets/app.js
  4. 11
      assets/bootstrap.js
  5. 4
      assets/controllers.json
  6. 16
      assets/controllers/hello_controller.js
  7. 3
      assets/styles/app.css
  8. 17
      bin/console
  9. 11
      bootstrap.php
  10. 103
      composer.json
  11. 5849
      composer.lock
  12. 14
      config/bundles.php
  13. 19
      config/packages/cache.yaml
  14. 5
      config/packages/debug.yaml
  15. 42
      config/packages/doctrine.yaml
  16. 6
      config/packages/doctrine_migrations.yaml
  17. 24
      config/packages/framework.yaml
  18. 61
      config/packages/monolog.yaml
  19. 12
      config/packages/routing.yaml
  20. 42
      config/packages/security.yaml
  21. 6
      config/packages/twig.yaml
  22. 15
      config/packages/web_profiler.yaml
  23. 49
      config/packages/webpack_encore.yaml
  24. 5
      config/preload.php
  25. 29
      config/routes.yaml
  26. 4
      config/routes/framework.yaml
  27. 8
      config/routes/web_profiler.yaml
  28. 24
      config/services.yaml
  29. 71
      lib/Framework/Controller.php
  30. 141
      lib/Framework/Kernel.php
  31. 17
      lib/Framework/Repository.php
  32. 57
      lib/Framework/Router.php
  33. 114
      lib/Framework/Twig.php
  34. 0
      migrations/.gitignore
  35. 818
      package-lock.json
  36. 19
      public/index.php
  37. 14
      src/Command/CleanupCommand.php
  38. 19
      src/Command/DaemonCommand.php
  39. 15
      src/Command/MailFailedRunsCommand.php
  40. 21
      src/Command/RunCommand.php
  41. 0
      src/Controller/.gitignore
  42. 83
      src/Controller/JobController.php
  43. 67
      src/Controller/SecurityController.php
  44. 17
      src/Controller/SiteController.php
  45. 0
      src/Entity/.gitignore
  46. 182
      src/Entity/Job.php
  47. 186
      src/Entity/Run.php
  48. 125
      src/Entity/User.php
  49. 11
      src/Kernel.php
  50. 0
      src/Repository/.gitignore
  51. 115
      src/Repository/JobRepository.php
  52. 20
      src/Repository/RunRepository.php
  53. 12
      src/Repository/UserRepository.php
  54. 6
      src/Service/Secret.php
  55. 70
      src/Twig/AppExtension.php
  56. 30
      storage/database.sql
  57. 388
      symfony.lock
  58. 2
      templates/job/add.html.twig
  59. 71
      templates/job/edit.html.twig
  60. 18
      templates/security/login.html.twig
  61. 28
      webcron

@ -1,3 +1,9 @@
################
### DEFAULT ###
################
## What kind of environment. Only use prod here.
APP_ENV=prod
################
### DATABASE ###
################
@ -57,4 +63,4 @@ MAILER_DSN=native://default
## Anonymous is still someone. So even if this someone is unknown you need someone who is sending your mails.
MAILER_FROM=www-data@example.com
## Now that everything is set up: go to your friends and get wasted!
## Now that everything is set up: go to your friends and get wasted!

29
.gitignore vendored

@ -1,10 +1,21 @@
cache/*
!cache/.gitkeep
vendor/
node_modules/
public/build/
webcron.old/config.inc.php
\.idea/
###> symfony/framework-bundle ###
/.env.local
/.env.local.php
/.env.*.local
/config/secrets/prod/prod.decrypt.private.php
/public/bundles/
/var/
/vendor/
###< symfony/framework-bundle ###
###> symfony/webpack-encore-bundle ###
/node_modules/
/public/build/
npm-debug.log
yarn-error.log
###< symfony/webpack-encore-bundle ###
.idea/
.env
storage/database.sqlite
.DS_Store

@ -0,0 +1,12 @@
/*
* Welcome to your app's main JavaScript file!
*
* We recommend including the built version of this JavaScript file
* (and its CSS file) in your base layout (base.html.twig).
*/
// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';
// start the Stimulus application
import './bootstrap';

@ -0,0 +1,11 @@
import { startStimulusApp } from '@symfony/stimulus-bridge';
// Registers Stimulus controllers from controllers.json and in the controllers/ directory
export const app = startStimulusApp(require.context(
'@symfony/stimulus-bridge/lazy-controller-loader!./controllers',
true,
/\.[jt]sx?$/
));
// register any custom, 3rd party controllers here
// app.register('some_controller_name', SomeImportedController);

@ -0,0 +1,4 @@
{
"controllers": [],
"entrypoints": []
}

@ -0,0 +1,16 @@
import { Controller } from '@hotwired/stimulus';
/*
* This is an example Stimulus controller!
*
* Any element with a data-controller="hello" attribute will cause
* this controller to be executed. The name "hello" comes from the filename:
* hello_controller.js -> "hello"
*
* Delete this file or adapt it for your use!
*/
export default class extends Controller {
connect() {
this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js';
}
}

@ -0,0 +1,3 @@
body {
background-color: lightgray;
}

@ -0,0 +1,17 @@
#!/usr/bin/env php
<?php
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
return new Application($kernel);
};

@ -1,11 +0,0 @@
<?php
require_once "vendor/autoload.php";
if( ini_get('safe_mode') ){
die("Cannot run in safe mode");
}
if (!file_exists(__DIR__ . "/.env")) {
die ("Cannot find config file");
}

@ -1,40 +1,87 @@
{
"name": "jeroened/webcron",
"description": "A simple webapp to manage webcrons",
"authors": [
{
"name": "Jeroen De Meerleer",
"email": "me@jeroened.be"
}
],
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"doctrine/dbal": "~3.2.0",
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-openssl": "*",
"ext-pcntl": "*",
"doctrine/doctrine-bundle": "^2.6",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.12",
"guzzlehttp/guzzle": "~7.4.0",
"mehrkanal/twig-encore-extension": "~1.3.0",
"phpseclib/phpseclib": "^3.0",
"symfony/config": "~6.0.0",
"symfony/console": "~6.0.0",
"symfony/dotenv": "~6.0.0",
"symfony/http-foundation": "~6.0.0",
"symfony/mailer": "~6.0.0",
"symfony/routing": "~6.0.0",
"symfony/yaml": "~6.0.0",
"twig/intl-extra": "~3.3.0",
"twig/twig": "~3.3.0",
"ext-pcntl": "*",
"ext-intl": "*",
"ext-openssl": "*"
"symfony/console": "6.0.*",
"symfony/dotenv": "6.0.*",
"symfony/flex": "^2",
"symfony/framework-bundle": "6.0.*",
"symfony/proxy-manager-bridge": "6.0.*",
"symfony/runtime": "6.0.*",
"symfony/security-bundle": "6.0.*",
"symfony/twig-bundle": "6.0.*",
"symfony/webpack-encore-bundle": "^1.14",
"symfony/yaml": "6.0.*"
},
"config": {
"allow-plugins": {
"composer/package-versions-deprecated": true,
"symfony/flex": true,
"symfony/runtime": true
},
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"JeroenED\\Framework\\": "lib/Framework/",
"JeroenED\\Webcron\\": "src/"
"App\\": "src/"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.0.*"
}
},
"require-dev": {
"symfony/debug-bundle": "6.0.*",
"symfony/maker-bundle": "^1.40",
"symfony/monolog-bundle": "^3.0",
"symfony/stopwatch": "6.0.*",
"symfony/web-profiler-bundle": "6.0.*"
}
}

5849
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,14 @@
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
];

@ -0,0 +1,19 @@
framework:
cache:
# Unique name of your app: used to compute stable namespaces for cache keys.
#prefix_seed: your_vendor_name/app_name
# The "app" cache stores to the filesystem by default.
# The data in this cache should persist between deploys.
# Other options include:
# Redis
#app: cache.adapter.redis
#default_redis_provider: redis://localhost
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
#app: cache.adapter.apcu
# Namespaced pools use the above "app" backend by default
#pools:
#my.dedicated.cache: null

@ -0,0 +1,5 @@
when@dev:
debug:
# Forwards VarDumper Data clones to a centralized server allowing to inspect dumps on CLI or in your browser.
# See the "server:dump" command to start a new server.
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"

@ -0,0 +1,42 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
when@test:
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
when@prod:
doctrine:
orm:
auto_generate_proxy_classes: false
query_cache_driver:
type: pool
pool: doctrine.system_cache_pool
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system

@ -0,0 +1,6 @@
doctrine_migrations:
migrations_paths:
# namespace is arbitrary but should be different from App\Migrations
# as migrations classes should NOT be autoloaded
'DoctrineMigrations': '%kernel.project_dir%/migrations'
enable_profiler: '%kernel.debug%'

@ -0,0 +1,24 @@
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
http_method_override: false
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.native
#esi: true
#fragments: true
php_errors:
log: true
when@test:
framework:
test: true
session:
storage_factory_id: session.storage.factory.mock_file

@ -0,0 +1,61 @@
monolog:
channels:
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
when@dev:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
when@test:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
channels: ["!event"]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
when@prod:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
nested:
type: stream
path: php://stderr
level: debug
formatter: monolog.formatter.json
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
deprecation:
type: stream
channels: [deprecation]
path: php://stderr

@ -0,0 +1,12 @@
framework:
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
when@prod:
framework:
router:
strict_requirements: null

@ -0,0 +1,42 @@
security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js|health)/
security: false
main:
pattern: ^\/(.*)
provider: app_user_provider
form_login:
login_path: /login
check_path: /login_check
enable_csrf: true
logout:
path: /logout
target: /
remember_me:
secret: '%kernel.secret%'
lifetime: 2419200 # 28 days in seconds
path: /
secure: true
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/(?!login|login_check|health)(?=.*), roles: ROLE_USER }
# - { path: ^/profile, roles: ROLE_USER }

@ -0,0 +1,6 @@
twig:
default_path: '%kernel.project_dir%/templates'
when@test:
twig:
strict_variables: true

@ -0,0 +1,15 @@
when@dev:
web_profiler:
toolbar: true
intercept_redirects: false
framework:
profiler: { only_exceptions: false }
when@test:
web_profiler:
toolbar: false
intercept_redirects: false
framework:
profiler: { collect: false }

@ -0,0 +1,49 @@
webpack_encore:
# The path where Encore is building the assets - i.e. Encore.setOutputPath()
output_path: '%kernel.project_dir%/public/build'
# If multiple builds are defined (as shown below), you can disable the default build:
# output_path: false
# Set attributes that will be rendered on all script and link tags
script_attributes:
defer: true
# Uncomment (also under link_attributes) if using Turbo Drive
# https://turbo.hotwired.dev/handbook/drive#reloading-when-assets-change
# 'data-turbo-track': reload
# link_attributes:
# Uncomment if using Turbo Drive
# 'data-turbo-track': reload
# If using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
# crossorigin: 'anonymous'
# Preload all rendered script and link tags automatically via the HTTP/2 Link header
# preload: true
# Throw an exception if the entrypoints.json file is missing or an entry is missing from the data
# strict_mode: false
# If you have multiple builds:
# builds:
# pass "frontend" as the 3rg arg to the Twig functions
# {{ encore_entry_script_tags('entry1', null, 'frontend') }}
# frontend: '%kernel.project_dir%/public/frontend/build'
# Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# Put in config/packages/prod/webpack_encore.yaml
# cache: true
framework:
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
#when@prod:
# webpack_encore:
# # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# # Available in version 1.2
# cache: true
#when@test:
# webpack_encore:
# strict_mode: false

@ -0,0 +1,5 @@
<?php
if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
}

@ -1,39 +1,34 @@
default:
path: '/'
defaults:
_controller: JeroenED\Webcron\Controller\JobController::defaultAction
_controller: App\Controller\JobController::defaultAction
health:
path: '/health'
defaults:
_controller: JeroenED\Webcron\Controller\SiteController::HealthAction
_controller: App\Controller\SiteController::healthAction
login:
path: '/login'
defaults:
_controller: JeroenED\Webcron\Controller\SecurityController::loginAction
logout:
path: '/logout'
defaults:
_controller: JeroenED\Webcron\Controller\SecurityController::logoutAction
_controller: App\Controller\SecurityController::loginAction
login_check:
path: '/login_check'
methods: ['POST']
defaults:
_controller: JeroenED\Webcron\Controller\SecurityController::loginCheckAction
logout:
path: '/logout'
job_index:
path: '/job'
defaults:
_controller: JeroenED\Webcron\Controller\JobController::defaultAction
_controller: App\Controller\JobController::defaultAction
job_view:
path: '/job/{id}/{all}'
methods: [ 'GET' ]
defaults:
_controller: JeroenED\Webcron\Controller\JobController::jobAction
_controller: App\Controller\JobController::jobAction
all: false
requirements:
id: \d+
@ -43,25 +38,25 @@ job_delete:
path: '/job/{id}'
methods: [ 'DELETE' ]
defaults:
_controller: JeroenED\Webcron\Controller\JobController::jobAction
_controller: App\Controller\JobController::jobAction
requirements:
id: \d+
job_edit:
path: '/job/{id}/edit'
defaults:
_controller: JeroenED\Webcron\Controller\JobController::editAction
_controller: App\Controller\JobController::editAction
requirements:
id: \d+
job_runnow:
path: '/job/{id}/runnow'
defaults:
_controller: JeroenED\Webcron\Controller\JobController::runnowAction
_controller: App\Controller\JobController::runNowAction
requirements:
id: \d+
job_add:
path: '/job/add'
defaults:
_controller: JeroenED\Webcron\Controller\JobController::addAction
_controller: App\Controller\JobController::addAction

@ -0,0 +1,4 @@
when@dev:
_errors:
resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
prefix: /_error

@ -0,0 +1,8 @@
when@dev:
web_profiler_wdt:
resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml'
prefix: /_wdt
web_profiler_profiler:
resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml'
prefix: /_profiler

@ -0,0 +1,24 @@
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

@ -1,71 +0,0 @@
<?php
namespace JeroenED\Framework;
use Doctrine\DBAL\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
abstract class Controller
{
private $twig;
private $request;
private $database;
private $kernel;
public function __construct(Request $request, Kernel $kernel)
{
$this->twig = new Twig($kernel);
$this->request = $request;
$this->kernel = $kernel;
}
public function getDbCon(): Connection
{
return $this->kernel->getDbCon();
}
/**
* @return Request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* @param Request $request
*/
public function setRequest(Request $request): void
{
$this->request = $request;
}
/**
* @param string $template
* @param array $vars
* @return Response
*/
public function render(string $template, array $vars = []): Response
{
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$vars['flashes'] = $_SESSION['flashes'] ?? [] ;
$_SESSION['flashes'] = [];
}
$response = new Response($this->twig->render($template, $vars));
return $response;
}
public function generateRoute(string $route): string
{
return $this->kernel->getRouter()->getUrlForRoute($route);
}
public function addFlash(string $category, string $content): void
{
$_SESSION['flashes'][] = ['category' => $category, 'content' => $content];
}
}

@ -1,141 +0,0 @@
<?php
namespace JeroenED\Framework;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use http\Exception\InvalidArgumentException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Dotenv\Dotenv;
class Kernel
{
private string $configDir;
private string $projectDir;
private string $templateDir;
private string $cacheDir;
private Router $router;
private ?Connection $dbCon = NULL;
/**
* @return string
*/
public function getConfigDir(): string
{
return $this->configDir;
}
/**
* @param string $configDir
*/
public function setConfigDir(string $configDir): void
{
$this->configDir = $configDir;
}
/**
* @return string
*/
public function getProjectDir(): string
{
return $this->projectDir;
}
/**
* @param string $projectDir
*/
public function setProjectDir(string $projectDir): void
{
$this->projectDir = $projectDir;
}
/**
* @return string
*/
public function getTemplateDir(): string
{
return $this->templateDir;
}
/**
* @param string $templateDir
*/
public function setTemplateDir(string $templateDir): void
{
$this->templateDir = $templateDir;
}
/**
* @return string
*/
public function getCacheDir(): string
{
return $this->cacheDir;
}
/**
* @param string $cacheDir
*/
public function setCacheDir(string $cacheDir): void
{
$this->cacheDir = $cacheDir;
}
/**
* @return Router
*/
public function getRouter(): Router
{
return $this->router;
}
public function handle(): Response
{
$this->router = new Router();
$this->router->parseRoutes($this->getConfigDir(), 'routes.yaml');
$request = $this->parseRequest();
if($request->isSecure()) {
ini_set('session.cookie_httponly', true);
ini_set('session.cookie_secure', true);
}
session_start();
return $this->router->route($request, $this);
}
public function parseDotEnv(string $path): void
{
$dotenv = new Dotenv();
$dotenv->loadEnv($path);
}
private function parseRequest(): Request
{
Request::setTrustedProxies(explode(',', $_ENV['TRUSTED_PROXIES']), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO);
$request = Request::createFromGlobals();
return $request;
}
public function getNewDbCon(): Connection {
if(!is_null($this->dbCon)) {
$this->dbCon->close();
$this->dbCon = null;
}
$this->dbCon = DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
return $this->dbCon;
}
public function getDbCon(): Connection
{
if(is_null($this->dbCon)) $this->dbCon = DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
return $this->dbCon;
}
}

@ -1,17 +0,0 @@
<?php
namespace JeroenED\Framework;
use Doctrine\DBAL\Connection;
class Repository
{
protected Connection $dbcon;
public function __construct(Connection $dbcon)
{
$this->dbcon = $dbcon;
}
}

@ -1,57 +0,0 @@
<?php
namespace JeroenED\Framework;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
class Router
{
private RouteCollection $routes;
private RequestContext $requestContext;
public function route(Request $request, Kernel $kernel): Response
{
$requestContext = new RequestContext();
$this->requestContext = $requestContext->fromRequest($request);
$matcher = new UrlMatcher($this->routes, $this->requestContext);
$method = $matcher->match($request->getPathInfo());
$controller = explode('::', $method['_controller']);
$controllerObj = new ('\\' . $controller[0])($request, $kernel);
$action = $controller[1];
unset($method['_controller']);
unset($method['_route']);
$response = $controllerObj->$action(...$method);
if ($response instanceof Response) {
$response->headers->add([
"Content-Security-Policy" => "default-src 'none'; font-src 'self'; style-src 'self'; script-src 'self'; connect-src 'self'; img-src 'self' data:; form-action 'self'; require-trusted-types-for 'script'; frame-ancestors 'none'; base-uri 'none'",
"Referrer-Policy" => "same-origin"
]);
return $response;
} else {
throw new InvalidArgumentException();
}
}
public function parseRoutes(string $dir, string $file): void
{
$routeloader = new YamlFileLoader(new FileLocator($dir));
$this->routes = $routeloader->load($file);
}
public function getUrlForRoute(string $route, array $params = []): string
{
$matcher = new UrlGenerator($this->routes, $this->requestContext);
return $matcher->generate($route, $params, UrlGenerator::ABSOLUTE_URL);
}
}

@ -1,114 +0,0 @@
<?php
namespace JeroenED\Framework;
use Mehrkanal\EncoreTwigExtension\Extensions\EntryFilesTwigExtension;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Twig\Cache\FilesystemCache;
use Twig\Environment;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
class Twig
{
private Environment $environment;
private Kernel $kernel;
public function __construct(Kernel $kernel)
{
$loader = new FilesystemLoader([$kernel->getTemplateDir()]);
$this->environment = new Environment($loader);
if($_ENV['DEBUG'] != 'true') {
$cache = new FilesystemCache($kernel->getCacheDir() . '/twig');
$this->environment->setCache($cache);
}
$this->kernel = $kernel;
$this->addExtensions();
$this->addFunctions();
$this->addFilters();
}
public function render(string $template, array $vars = []): string
{
return $this->environment->render($template, $vars);
}
public function addExtensions()
{
$this->environment->addExtension(new IntlExtension());
$this->environment->addExtension(new EntryFilesTwigExtension(new EntrypointLookup('./public/build/entrypoints.json')));
}
public function addFunctions()
{
$path = new TwigFunction('path', function(string $route, array $params = []) {
return $this->kernel->getRouter()->getUrlForRoute($route, $params);
});
$this->environment->addFunction($path);
}
public function addFilters() {
$secondsToInterval = new TwigFilter('interval', function(int|float $time) {
$return = '';
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$return .= ($days != 0 || !empty($return)) ? "{$days}d " : '';
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$return .= ($hours != 0 || !empty($return)) ? "{$hours}h " : '';
$minutes = floor($time / 60);
$time -= $minutes * 60;
$return .= ($minutes != 0 || !empty($return)) ? "{$minutes}m " : '';
$time = round($time, 3);
$return .= ($time != 0 || !empty($return)) ? "{$time}s " : '';
return $return;
});
$parseTags = new TwigFilter('parsetags', function(string $text) {
$results = [];
preg_match_all('/\[([A-Za-z0-9 \-]+)\]/', $text, $results);
foreach ($results[0] as $key=>$result) {
$background = substr(md5($results[0][$key]), 0, 6);
$color = $this->lightOrDark($background) == 'dark' ? 'ffffff' : '000000';
$text = str_replace($results[0][$key], '<span class="tag" data-background-color="#' . $background . '" data-color="#' . $color . '">' . $results[1][$key] . '</span>', $text);
}
return $text;
});
$this->environment->addFilter($secondsToInterval);
$this->environment->addFilter($parseTags);
}
private function lightOrDark ($color) {
$color = str_split($color, 2);
foreach($color as &$value) {
$value = hexdec($value);
}
// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
$hsp = sqrt(
0.299 * ($color[0] * $color[0]) +
0.587 * ($color[1] * $color[1]) +
0.114 * ($color[2] * $color[2])
);
// Using the HSP value, determine whether the color is light or dark
if ($hsp>140) {
return 'light';
} else {
return 'dark';
}
}
}

818
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,18 +1,9 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
use JeroenED\Framework\Kernel;
chdir(__DIR__ . '/..');
require_once 'bootstrap.php';
use App\Kernel;
$kernel = new Kernel();
$kernel->setProjectDir(getcwd());
$kernel->setConfigDir(getcwd() . '/config/');
$kernel->setTemplateDir(getcwd() . '/templates/');
$kernel->setCacheDir(getcwd() . '/cache/');
$kernel->parseDotEnv($kernel->getProjectDir() . '/.env');
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
ini_set('date.timezone', $_ENV['TZ']);
$kernel->handle()->send();
return function (array $context) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

@ -1,25 +1,29 @@
<?php
namespace JeroenED\Webcron\Command;
namespace App\Command;
use App\Entity\Run;
use Doctrine\DBAL\Exception;
use JeroenED\Framework\Kernel;
use JeroenED\Webcron\Repository\Run;
use App\Repository\RunRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class CleanupCommand extends Command
{
protected static $defaultName = 'cleanup';
protected $kernel;
protected $doctrine;
public function __construct(Kernel $kernel)
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
{
$this->kernel = $kernel;
$this->doctrine = $doctrine;
parent::__construct();
}
@ -36,7 +40,7 @@ class CleanupCommand extends Command
{
$maxage = $input->getOption('maxage');
$jobs = $input->getOption('jobid');
$runRepo = new Run($this->kernel->getDbCon());
$runRepo = $this->doctrine->getRepository(Run::class);
try {
$deleted = $runRepo->cleanupRuns($jobs, $maxage);
$output->writeln('Deleted ' . $deleted . ' runs');

@ -1,14 +1,16 @@
<?php
namespace JeroenED\Webcron\Command;
namespace App\Command;
use JeroenED\Framework\Kernel;
use JeroenED\Webcron\Repository\Job;
use App\Entity\Job;
use App\Repository\JobRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class DaemonCommand extends Command
@ -16,10 +18,13 @@ class DaemonCommand extends Command
protected static $defaultName = 'daemon';
protected $kernel;
protected $doctrine;
public function __construct(Kernel $kernel)
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
{
$this->kernel = $kernel;
$this->doctrine = $doctrine;
parent::__construct();
}
@ -33,7 +38,7 @@ class DaemonCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$jobRepo = new Job($this->kernel->getDbCon());
$jobRepo = $this->doctrine->getRepository(Job::class);
$timelimit = $input->getOption('time-limit') ?? false;
if ($timelimit === false) {
$endofscript = false;
@ -63,8 +68,8 @@ class DaemonCommand extends Command
declare(ticks = 1);
pcntl_signal(SIGCHLD, SIG_IGN);
$pid = pcntl_fork();
$jobRepo = NULL;
$jobRepo = new Job($this->kernel->getNewDbCon());
$this->doctrine->getConnection()->close();
$jobRepo = $this->doctrine->getRepository(Job::class);
if($pid == -1) {
$jobRepo->RunJob($job['id'], $job['running'] == 2);
$jobRepo->setJobRunning($job['id'], false);

@ -1,14 +1,13 @@
<?php
namespace JeroenED\Webcron\Command;
namespace App\Command;
use JeroenED\Framework\Kernel;
use JeroenED\Framework\Twig;
use JeroenED\Webcron\Repository\Job;
use JeroenED\Webcron\Repository\User;
use App\Repository\JobRepository;
use App\Repository\UserRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
@ -19,7 +18,7 @@ class MailFailedRunsCommand extends Command
protected static $defaultName = 'mail-failed-runs';
protected $kernel;
public function __construct(Kernel $kernel)
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
parent::__construct();
@ -35,8 +34,8 @@ class MailFailedRunsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$userRepo = new User($this->kernel->getDbCon());
$jobRepo = new Job($this->kernel->getDbCon());
$userRepo = new UserRepository($this->kernel->getDbCon());
$runRepo = $this->getEntityManager()->getRepository(Run::class);
$failedJobs = $jobRepo->getFailingJobs();

@ -1,23 +1,22 @@
<?php
namespace JeroenED\Webcron\Command;
namespace App\Command;
use JeroenED\Framework\Kernel;
use JeroenED\Framework\Repository;
use JeroenED\Webcron\Repository\Job;
use JeroenED\Webcron\Repository\Run;
use App\Repository\JobRepository;
use App\Repository\RunRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class RunCommand extends Command
{
protected static $defaultName = 'run';
protected $kernel;
public function __construct(Kernel $kernel)
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
parent::__construct();
@ -26,18 +25,18 @@ class RunCommand extends Command
protected function configure()
{
$this
->setDescription('Run a single cronjob')
->setDescription('RunRepository a single cronjob')
->setHelp('This command runs a single command')
->addArgument('jobid', InputArgument::REQUIRED, 'The id of the job to be run');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$jobRepo = new Job($this->kernel->getDbCon());
$runRepo = $this->getEntityManager()->getRepository(Run::class);
$jobId = (int)$input->getArgument('jobid');
$jobRunning = $jobRepo->isLockedJob($jobId);
if($jobRunning) {
$output->writeln('Job is already running');
$output->writeln('JobRepository is already running');
return Command::FAILURE;
}
$jobRepo->setJobRunning($jobId, true);
@ -57,10 +56,10 @@ class RunCommand extends Command
$jobRepo->setTempVar($jobId, 'consolerun', false);
$output->write($result['output']);
if($result['success']) {
$output->writeln('Job succeeded with in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
$output->writeln('JobRepository succeeded with in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
return Command::SUCCESS;
} else {
$output->writeln('Job failed in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
$output->writeln('JobRepository failed in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
return Command::FAILURE;
}
}

@ -1,101 +1,86 @@
<?php
namespace JeroenED\Webcron\Controller;
namespace App\Controller;
use JeroenED\Framework\Controller;
use JeroenED\Framework\Repository;
use JeroenED\Webcron\Repository\Job;
use JeroenED\Webcron\Repository\Run;
use App\Entity\Job;
use App\Entity\Run;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class JobController extends Controller
class JobController extends AbstractController
{
public function DefaultAction()
public function defaultAction(ManagerRegistry $doctrine): Response
{
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
$jobRepo = new Job($this->getDbCon());
$jobRepo = $doctrine->getRepository(Job::class);
$jobs = $jobRepo->getAllJobs();
return $this->render('job/index.html.twig', ['jobs' => $jobs]);
}
public function jobAction($id, $all = false)
public function jobAction(Request $request, ManagerRegistry $doctrine, $id, $all = false): Response
{
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
$jobRepo = new Job($this->getDbCon());
$runRepo = new Run($this->getDbCon());
$jobRepo = $doctrine->getRepository(Job::class);
$runRepo = $doctrine->getRepository(Run::class);
if($this->getRequest()->getMethod() == 'GET') {
if($request->getMethod() == 'GET') {
$job = $jobRepo->getJob($id);
$runs = $runRepo->getRunsForJob($id, $all != 'all');
return $this->render('job/view.html.twig', ['job' => $job, 'runs' => $runs, 'allruns' => $all == 'all']);
} elseif($this->getRequest()->getMethod() == 'DELETE') {
} elseif($request->getMethod() == 'DELETE') {
$success = $jobRepo->deleteJob($id);
$this->addFlash('success', $success['message']);
return new JsonResponse(['return_path' => $this->generateRoute('job_index')]);
return new JsonResponse(['return_path' => $this->GenerateUrl('job_index')]);
}
}
public function editAction($id)
public function editAction(Request $request, ManagerRegistry $doctrine, $id)
{
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
if($this->getRequest()->getMethod() == 'GET') {
$jobRepo = new Job($this->getDbCon());
if($request->getMethod() == 'GET') {
$jobRepo = $doctrine->getRepository(Job::class);
$job = $jobRepo->getJob($id, true);
return $this->render('job/edit.html.twig', $job);
} elseif($this->getRequest()->getMethod() == 'POST') {
$allValues = $this->getRequest()->request->all();
$jobRepo = new Job($this->getDbCon());
} elseif($request->getMethod() == 'POST') {
$allValues = $request->request->all();
$jobRepo = $doctrine->getRepository(Job::class);
try {
$joboutput = $jobRepo->editJob($id, $allValues);
} catch (\InvalidArgumentException $e) {
$this->addFlash('danger', $e->getMessage());
return new RedirectResponse($this->generateRoute('job_edit', ['id' => $allValues['id']]));
return new RedirectResponse($this->GenerateUrl('job_edit', ['id' => $allValues['id']]));
}
$this->addFlash('success', $joboutput['message']);
return new RedirectResponse($this->generateRoute('job_index'));
return new RedirectResponse($this->GenerateUrl('job_index'));
}
}
public function addAction()
public function addAction(Request $request, ManagerRegistry $doctrine)
{
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
if($this->getRequest()->getMethod() == 'GET') {
return $this->render('job/add.html.twig');
} elseif ($this->getRequest()->getMethod() == 'POST') {
$allValues = $this->getRequest()->request->all();
$jobRepo = new Job($this->getDbCon());
if($request->getMethod() == 'GET') {
return $this->render('job/add.html.twig', ['data' => []]);
} elseif ($request->getMethod() == 'POST') {
$allValues = $request->request->all();
$jobRepo = $doctrine->getRepository(Job::class);
try {
$joboutput = $jobRepo->addJob($allValues);
} catch (\InvalidArgumentException $e) {