Better structure

This commit is contained in:
Jeroen De Meerleer 2021-04-06 22:20:18 +02:00
parent cce35b24bb
commit 76c27c703c
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
12 changed files with 151 additions and 61 deletions

View File

@ -1,12 +1,22 @@
# Webcron Management
(c) 2017 Jeroen De Meerleer <me@jeroened.be>
(c) 2017, 2021 Jeroen De Meerleer <me@jeroened.be>
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.

View File

@ -1,21 +1,8 @@
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
use Doctrine\DBAL\DriverManager;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Yaml\Yaml;
session_start();
require_once "vendor/autoload.php";
require_once "include/functions.php";
if( ini_get('safe_mode') ){
die("Cannot run in safe mode");
@ -23,26 +10,4 @@ if( ini_get('safe_mode') ){
if (!file_exists(__DIR__ . "/.env")) {
die ("Cannot find config file");
}
$path = __DIR__.'/.env';
$dotenv = new Dotenv();
$dotenv->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();
}

View File

@ -1,3 +1,13 @@
<?php
use JeroenED\Framework\Kernel;
require_once 'bootstrap.php';
$kernel = new Kernel();
$kernel->setProjectDir(__DIR__);
$kernel->setConfigDir(__DIR__ . '/config/');
$kernel->setTemplateDir(__DIR__ . '/templates/');
$db = DriverManager::getConnection(['url' => $_ENV['DATABASE']]);
$kernel->handle()->send();

View File

@ -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;
}
/**

106
lib/Framework/Kernel.php Normal file
View File

@ -0,0 +1,106 @@
<?php
namespace JeroenED\Framework;
use http\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Yaml\Yaml;
class Kernel
{
private string $configDir;
private string $projectDir;
private string $templateDir;
/**
* @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;
}
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();
}
}
}