From 76c27c703c8c9d9ec38028f5a7a0ac9d951cbdba Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 6 Apr 2021 22:20:18 +0200 Subject: [PATCH] Better structure --- README.md | 36 +++--- bootstrap.php | 37 +----- index.php | 10 ++ lib/Framework/Controller.php | 23 +++- lib/Framework/Kernel.php | 106 ++++++++++++++++++ .../templates => templates}/addjob.html.twig | 0 .../templates => templates}/base.html.twig | 0 .../templates => templates}/config.html.twig | 0 .../templates => templates}/editjob.html.twig | 0 .../overview.html.twig | 0 .../templates => templates}/runs.html.twig | 0 .../security/login.html.twig | 0 12 files changed, 151 insertions(+), 61 deletions(-) create mode 100644 lib/Framework/Kernel.php rename {src/Resources/templates => templates}/addjob.html.twig (100%) rename {src/Resources/templates => templates}/base.html.twig (100%) rename {src/Resources/templates => templates}/config.html.twig (100%) rename {src/Resources/templates => templates}/editjob.html.twig (100%) rename {src/Resources/templates => templates}/overview.html.twig (100%) rename {src/Resources/templates => templates}/runs.html.twig (100%) rename {src/Resources/templates => templates}/security/login.html.twig (100%) diff --git a/README.md b/README.md index 424d062..4af2f22 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,22 @@ # Webcron Management -(c) 2017 Jeroen De Meerleer +(c) 2017, 2021 Jeroen De Meerleer Webcron management is an easy-to-use interface to manage cronjob running on a publicly available http-location. +## Status update +I'm currently in the process of rewriting the application to more modern standards. The current main branch is very unstable at the moment. Please don't use it. + +I encourage everyone to wait for the new version as upgrading will probably be very difficult. + +### What will change with the rewrite? +* All urls will change. eg. /login/ and /jobs/5/edit/ instead of /login.php and editjob.php?jobId=5 +* Dropping support for directly calling webcron.php from url-bar +* Daemonized main-script which will enable running cronjobs by seconds + ## Requirements * Webserver able to run PHP -* PHP 7.0 or greater -* MySQL/MariaDB +* PHP 8.0 or greater +* MySQL/MariaDB (Or sqLite) * Ability to add a system cronjob for installation (You can maybe ask you webhost?) ## Instalation @@ -35,23 +45,3 @@ TL;DR ### Can I schedule a reboot every week? Yes, you can do this by creating a job with `reboot` as "url". When this job needs to run, the reboot is triggered to run at the very end. At the first run of the master script a list of active and terribly failed services is pushed to the job so you can check this if something is wrong. - -## Licence - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. diff --git a/bootstrap.php b/bootstrap.php index 4cb6f2b..9fecaf9 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -1,21 +1,8 @@ loadEnv($path); - -$yaml = Yaml::parseFile('config/routes.yaml'); -$routeloader = new YamlFileLoader(new FileLocator([__DIR__])); -$routes = $routeloader->load('config/routes.yaml'); - -$request = Request::createFromGlobals(); -$requestContext = RequestContext::fromUri($request->getUri()); -$matcher = new UrlMatcher($routes, $requestContext); -$method = $matcher->match($request->getPathInfo()); - -$db = DriverManager::getConnection(['url' => $_ENV['DATABASE']]); - -$controller = explode('::', $method['_controller']); -$controllerObj = new ('\\' . $controller[0]); -$action = $controller[1]; -$response = $controllerObj->$action(); - -$response->send(); +} \ No newline at end of file diff --git a/index.php b/index.php index 5bf976d..9abb4cc 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,13 @@ setProjectDir(__DIR__); +$kernel->setConfigDir(__DIR__ . '/config/'); +$kernel->setTemplateDir(__DIR__ . '/templates/'); + +$db = DriverManager::getConnection(['url' => $_ENV['DATABASE']]); +$kernel->handle()->send(); \ No newline at end of file diff --git a/lib/Framework/Controller.php b/lib/Framework/Controller.php index b1582c9..e8f3b3a 100644 --- a/lib/Framework/Controller.php +++ b/lib/Framework/Controller.php @@ -3,6 +3,7 @@ namespace JeroenED\Framework; +use http\Env\Request; use Symfony\Component\HttpFoundation\Response; use Twig\Environment; use Twig\Loader\FilesystemLoader; @@ -10,11 +11,29 @@ use Twig\Loader\FilesystemLoader; abstract class Controller { private $twig; + private $request; - public function __construct() + public function __construct(Request $request, Kernel $kernel) { - $loader = new FilesystemLoader(['src/Resources/templates']); + $loader = new FilesystemLoader([$kernel->getTemplateDir()]); $this->twig = new Environment($loader); + $this->request = $request; + } + + /** + * @return Request + */ + public function getRequest(): Request + { + return $this->request; + } + + /** + * @param Request $request + */ + public function setRequest(Request $request): void + { + $this->request = $request; } /** diff --git a/lib/Framework/Kernel.php b/lib/Framework/Kernel.php new file mode 100644 index 0000000..c01b12d --- /dev/null +++ b/lib/Framework/Kernel.php @@ -0,0 +1,106 @@ +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; + } + + public function handle(): Response + { + $this->parseDotEnv($this->getProjectDir() . '/.env'); + $routes = $this->parseRoutes($this->getConfigDir(), '/routes.yaml'); + $request = $this->parseRequest(); + return $this->createResponse($request, $routes); + } + + private function parseDotEnv(string $path): void + { + $dotenv = new Dotenv(); + $dotenv->loadEnv($path); + } + + private function parseRoutes(string $dir, string $file): RouteCollection + { + $routeloader = new YamlFileLoader(new FileLocator($dir)); + return $routeloader->load($file); + } + + private function parseRequest(): Request + { + return Request::createFromGlobals(); + } + + private function createResponse($request, $routes): Response + { + $requestContext = RequestContext::fromUri($request->getUri()); + $matcher = new UrlMatcher($routes, $requestContext); + $method = $matcher->match($request->getPathInfo()); + $controller = explode('::', $method['_controller']); + $controllerObj = new ('\\' . $controller[0])($request, $this); + $action = $controller[1]; + $response = $controllerObj->$action(); + + if ($response instanceof Response) { + return $response; + } else { + throw new InvalidArgumentException(); + } + } +} \ No newline at end of file diff --git a/src/Resources/templates/addjob.html.twig b/templates/addjob.html.twig similarity index 100% rename from src/Resources/templates/addjob.html.twig rename to templates/addjob.html.twig diff --git a/src/Resources/templates/base.html.twig b/templates/base.html.twig similarity index 100% rename from src/Resources/templates/base.html.twig rename to templates/base.html.twig diff --git a/src/Resources/templates/config.html.twig b/templates/config.html.twig similarity index 100% rename from src/Resources/templates/config.html.twig rename to templates/config.html.twig diff --git a/src/Resources/templates/editjob.html.twig b/templates/editjob.html.twig similarity index 100% rename from src/Resources/templates/editjob.html.twig rename to templates/editjob.html.twig diff --git a/src/Resources/templates/overview.html.twig b/templates/overview.html.twig similarity index 100% rename from src/Resources/templates/overview.html.twig rename to templates/overview.html.twig diff --git a/src/Resources/templates/runs.html.twig b/templates/runs.html.twig similarity index 100% rename from src/Resources/templates/runs.html.twig rename to templates/runs.html.twig diff --git a/src/Resources/templates/security/login.html.twig b/templates/security/login.html.twig similarity index 100% rename from src/Resources/templates/security/login.html.twig rename to templates/security/login.html.twig