Cleaned codebase.
This commit is contained in:
parent
40950feb27
commit
c0694a72e8
12 changed files with 131 additions and 120 deletions
104
src/App.php
104
src/App.php
|
|
@ -28,15 +28,12 @@ use Psr\Container\ContainerInterface;
|
|||
use Psr\Http\Message\ResponseInterface;
|
||||
use Slim;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Twig;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
class App
|
||||
{
|
||||
public const DEFAULT_TIMEZONE = 'Europe/London';
|
||||
|
||||
private static bool $isInitialised = false;
|
||||
public static App $instance;
|
||||
|
||||
protected EnvironmentService $environmentService;
|
||||
|
|
@ -48,6 +45,8 @@ class App
|
|||
protected array $viewPaths = [];
|
||||
protected bool $interrogateControllersComplete = false;
|
||||
|
||||
private static bool $isInitialised = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Configure Dependency Injector
|
||||
|
|
@ -69,15 +68,6 @@ class App
|
|||
$errorMiddleware = $this->app->addErrorMiddleware(true, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item from Dependency Injection
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $id){
|
||||
return $this->getApp()->getContainer()->get($id);
|
||||
}
|
||||
|
||||
protected function setup(ContainerInterface $container): void
|
||||
{
|
||||
$this->logger = $container->get(Logger::class);
|
||||
|
|
@ -92,6 +82,16 @@ class App
|
|||
$this->interrogateControllers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item from Dependency Injection.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $id)
|
||||
{
|
||||
return $this->getApp()->getContainer()->get($id);
|
||||
}
|
||||
|
||||
public function setupContainer(): Container
|
||||
{
|
||||
$app = $this;
|
||||
|
|
@ -99,11 +99,12 @@ class App
|
|||
(new ContainerBuilder())
|
||||
->useAutowiring(true)
|
||||
->useAnnotations(true)
|
||||
#->enableCompilation(APP_ROOT . "/cache")
|
||||
#->writeProxiesToFile(true, APP_ROOT . "/cache/injection-proxies")
|
||||
->build();
|
||||
//->enableCompilation(APP_ROOT . "/cache")
|
||||
//->writeProxiesToFile(true, APP_ROOT . "/cache/injection-proxies")
|
||||
->build()
|
||||
;
|
||||
|
||||
$container->set(Slim\Views\Twig::class, function(ContainerInterface $container) {
|
||||
$container->set(Slim\Views\Twig::class, function (ContainerInterface $container) {
|
||||
foreach ($this->viewPaths as $i => $viewLocation) {
|
||||
if (!file_exists($viewLocation) || !is_dir($viewLocation)) {
|
||||
unset($this->viewPaths[$i]);
|
||||
|
|
@ -113,7 +114,7 @@ class App
|
|||
$loader = new FilesystemLoader();
|
||||
|
||||
foreach ($this->viewPaths as $path) {
|
||||
$loader->addPath($path);
|
||||
$loader->addPath($path);
|
||||
}
|
||||
|
||||
$twig = new Slim\Views\Twig($loader, $settings);
|
||||
|
|
@ -135,22 +136,22 @@ class App
|
|||
|
||||
return $twig;
|
||||
});
|
||||
$container->set('view', function(ContainerInterface $container){
|
||||
$container->set('view', function (ContainerInterface $container) {
|
||||
return $container->get(Slim\Views\Twig::class);
|
||||
});
|
||||
|
||||
$container->set(EnvironmentService::class, function(ContainerInterface $container){
|
||||
$container->set(EnvironmentService::class, function (ContainerInterface $container) {
|
||||
return new EnvironmentService();
|
||||
});
|
||||
|
||||
$container->set(ConfigurationService::class, function(ContainerInterface $container) use ($app){
|
||||
$container->set(ConfigurationService::class, function (ContainerInterface $container) use ($app) {
|
||||
return new ConfigurationService(
|
||||
$app,
|
||||
$container->get(EnvironmentService::class)
|
||||
);
|
||||
});
|
||||
|
||||
$container->set(\Faker\Generator::class, function(ContainerInterface $c) {
|
||||
$container->set(\Faker\Generator::class, function (ContainerInterface $c) {
|
||||
$faker = FakerFactory::create();
|
||||
$faker->addProvider(new Provider\Base($faker));
|
||||
$faker->addProvider(new Provider\DateTime($faker));
|
||||
|
|
@ -164,7 +165,7 @@ class App
|
|||
|
||||
return $faker;
|
||||
});
|
||||
$container->set(CachePoolChain::class, function(ContainerInterface $c) {
|
||||
$container->set(CachePoolChain::class, function (ContainerInterface $c) {
|
||||
$caches = [];
|
||||
|
||||
// If apc/apcu present, add it to the pool
|
||||
|
|
@ -181,7 +182,7 @@ class App
|
|||
return new CachePoolChain($caches);
|
||||
});
|
||||
|
||||
$container->set('MonologFormatter', function(ContainerInterface $c) {
|
||||
$container->set('MonologFormatter', function (ContainerInterface $c) {
|
||||
/** @var Services\EnvironmentService $environment */
|
||||
$environment = $c->get(Services\EnvironmentService::class);
|
||||
|
||||
|
|
@ -193,7 +194,7 @@ class App
|
|||
);
|
||||
});
|
||||
|
||||
$container->set(Logger::class, function(ContainerInterface $c) {
|
||||
$container->set(Logger::class, function (ContainerInterface $c) {
|
||||
/** @var ConfigurationService $configuration */
|
||||
$configuration = $c->get(ConfigurationService::class);
|
||||
|
||||
|
|
@ -204,7 +205,7 @@ class App
|
|||
return $monolog;
|
||||
});
|
||||
|
||||
$container->set(DebugBar::class, function(ContainerInterface $container) {
|
||||
$container->set(DebugBar::class, function (ContainerInterface $container) {
|
||||
$debugBar = new StandardDebugBar();
|
||||
/** @var Logger $logger */
|
||||
$logger = $container->get(Logger::class);
|
||||
|
|
@ -213,42 +214,41 @@ class App
|
|||
return $debugBar;
|
||||
});
|
||||
|
||||
$container->set(\Middlewares\Debugbar::class, function(ContainerInterface $container) {
|
||||
$container->set(\Middlewares\Debugbar::class, function (ContainerInterface $container) {
|
||||
$debugBar = $container->get(DebugBar::class);
|
||||
|
||||
return new \Middlewares\Debugbar($debugBar);
|
||||
});
|
||||
|
||||
$container->set(\Redis::class, function(ContainerInterface $container){
|
||||
$container->set(\Redis::class, function (ContainerInterface $container) {
|
||||
$environmentService = $container->get(EnvironmentService::class);
|
||||
|
||||
$redis = new \Redis();
|
||||
$redis->connect(
|
||||
$environmentService->get('REDIS_HOST', 'redis'),
|
||||
$environmentService->get('REDIS_PORT', 6379)
|
||||
);
|
||||
$redis = new \Redis();
|
||||
$redis->connect(
|
||||
$environmentService->get('REDIS_HOST', 'redis'),
|
||||
$environmentService->get('REDIS_PORT', 6379)
|
||||
);
|
||||
|
||||
return $redis;
|
||||
return $redis;
|
||||
});
|
||||
|
||||
$container->set(SessionService::class, function(ContainerInterface $container){
|
||||
$container->set(SessionService::class, function (ContainerInterface $container) {
|
||||
return new SessionService(
|
||||
$container->get(\Redis::class)
|
||||
);
|
||||
});
|
||||
|
||||
$container->set(Databases::class, function(ContainerInterface $container){
|
||||
$container->set(Databases::class, function (ContainerInterface $container) {
|
||||
return new Databases(
|
||||
$container->get(ConfigurationService::class)
|
||||
);
|
||||
|
||||
});
|
||||
$container->set(Laminator::class, function(ContainerInterface $container){
|
||||
return new Laminator(
|
||||
APP_ROOT,
|
||||
$container->get(ConfigurationService::class),
|
||||
$container->get(Databases::class)
|
||||
);
|
||||
$container->set(Laminator::class, function (ContainerInterface $container) {
|
||||
return new Laminator(
|
||||
APP_ROOT,
|
||||
$container->get(ConfigurationService::class),
|
||||
$container->get(Databases::class)
|
||||
);
|
||||
});
|
||||
|
||||
/** @var Services\EnvironmentService $environmentService */
|
||||
|
|
@ -269,15 +269,15 @@ class App
|
|||
public function setupMiddlewares(ContainerInterface $container): void
|
||||
{
|
||||
// Middlewares
|
||||
#$this->app->add($container->get(Middleware\EnvironmentHeadersOnResponse::class));
|
||||
#$this->app->add($container->get(\Middlewares\ContentLength::class));
|
||||
#$this->app->add($container->get(\Middlewares\Debugbar::class));
|
||||
#$this->app->add($container->get(\Middlewares\Geolocation::class));
|
||||
#$this->app->add($container->get(\Middlewares\TrailingSlash::class));
|
||||
#$this->app->add($container->get(Middleware\JSONResponseLinter::class));
|
||||
#$this->app->add($container->get(\Middlewares\Whoops::class));
|
||||
#$this->app->add($container->get(\Middlewares\Minifier::class));
|
||||
#$this->app->add($container->get(\Middlewares\GzipEncoder::class));
|
||||
//$this->app->add($container->get(Middleware\EnvironmentHeadersOnResponse::class));
|
||||
//$this->app->add($container->get(\Middlewares\ContentLength::class));
|
||||
//$this->app->add($container->get(\Middlewares\Debugbar::class));
|
||||
//$this->app->add($container->get(\Middlewares\Geolocation::class));
|
||||
//$this->app->add($container->get(\Middlewares\TrailingSlash::class));
|
||||
//$this->app->add($container->get(Middleware\JSONResponseLinter::class));
|
||||
//$this->app->add($container->get(\Middlewares\Whoops::class));
|
||||
//$this->app->add($container->get(\Middlewares\Minifier::class));
|
||||
//$this->app->add($container->get(\Middlewares\GzipEncoder::class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -406,7 +406,7 @@ class App
|
|||
$this->interrogateControllersComplete = true;
|
||||
|
||||
$controllerPaths = [
|
||||
APP_ROOT . '/src/Controllers',
|
||||
APP_ROOT.'/src/Controllers',
|
||||
];
|
||||
|
||||
foreach ($controllerPaths as $controllerPath) {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ use ⌬\Filters\Filter;
|
|||
|
||||
abstract class Controller
|
||||
{
|
||||
|
||||
/** @var Service */
|
||||
protected $service;
|
||||
/** @var bool */
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Benzine\Controllers;
|
||||
|
||||
use Benzine\ORM\Interfaces\ModelInterface;
|
||||
use Laminas\Db\Adapter\Exception\InvalidQueryException;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Benzine\ORM\Interfaces\ModelInterface;
|
||||
|
||||
abstract class CrudController extends Controller
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,11 +5,9 @@ namespace Benzine\Controllers;
|
|||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Slim\Views\Twig;
|
||||
use ⌬\Traits\RenderHtmlTrait;
|
||||
|
||||
abstract class HtmlController extends Controller
|
||||
{
|
||||
|
||||
/** @var Twig */
|
||||
protected $twig;
|
||||
|
||||
|
|
@ -35,7 +33,7 @@ abstract class HtmlController extends Controller
|
|||
if ('.json' == substr($request->getUri()->getPath(), -5, 5)) {
|
||||
return $this->jsonResponse($parameters, $request, $response);
|
||||
}
|
||||
|
||||
|
||||
return $this->twig->render(
|
||||
$response,
|
||||
$template,
|
||||
|
|
|
|||
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
namespace Benzine\Middleware;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
use Benzine\Configuration;
|
||||
use Benzine\ORM\Profiler;
|
||||
use Benzine\⌬;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
class EnvironmentHeadersOnResponse
|
||||
{
|
||||
|
||||
protected $apiExplorerEnabled = true;
|
||||
|
||||
/** @var Configuration\Configuration */
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace Benzine\Router;
|
||||
|
||||
use Slim\App;
|
||||
use Monolog\Logger;
|
||||
use Slim\App;
|
||||
|
||||
class Router
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
namespace Benzine\Services;
|
||||
|
||||
use Benzine\App;
|
||||
use Benzine\ORM\Connection\Database;
|
||||
use Benzine\ORM\Connection\Databases;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class ConfigurationService
|
||||
|
|
@ -32,54 +30,19 @@ class ConfigurationService
|
|||
$this->setupDefines();
|
||||
}
|
||||
|
||||
protected function setupDefines() : void
|
||||
public function has(string $key): bool
|
||||
{
|
||||
define("APP_ROOT", $this->appRoot);
|
||||
define("APP_NAME", $this->get('application/name'));
|
||||
return null !== $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate .benzine.yml
|
||||
* @param string|null $path
|
||||
* @param string $key
|
||||
* @param null|string $defaultValue
|
||||
*
|
||||
* @return null|array|string
|
||||
*/
|
||||
protected function findConfig(string $path = null) : bool {
|
||||
if(!$path){
|
||||
$path = getcwd();
|
||||
//$path = dirname($this->environmentService->get('SCRIPT_FILENAME'));
|
||||
}
|
||||
if(!file_exists($path . "/.benzine.yml")){
|
||||
$currentDirElem = explode(DIRECTORY_SEPARATOR, $path);
|
||||
array_pop($currentDirElem);
|
||||
$parentPath = implode(DIRECTORY_SEPARATOR, $currentDirElem);
|
||||
return $this->findConfig($parentPath);
|
||||
}
|
||||
|
||||
$this->parseFile($path . "/.benzine.yml");
|
||||
$this->appRoot = $path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function parseFile(string $file)
|
||||
public function get(string $key, string $defaultValue = null)
|
||||
{
|
||||
$yaml = file_get_contents($file);
|
||||
foreach($this->environmentService->all() as $key => $value){
|
||||
if(is_string($value)) {
|
||||
$yaml = str_replace("\$$key", $value, $yaml);
|
||||
}
|
||||
}
|
||||
$this->config = Yaml::parse($yaml);
|
||||
}
|
||||
|
||||
public function has(string $key) : bool {
|
||||
return $this->get($key) !== null;
|
||||
}
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string|null $defaultValue
|
||||
* @return string|array|null
|
||||
*/
|
||||
public function get(string $key, string $defaultValue = null){
|
||||
$scope = $this->config;
|
||||
foreach (explode('/', strtolower($key)) as $keyBit) {
|
||||
$scope = &$scope[$keyBit];
|
||||
|
|
@ -98,9 +61,11 @@ class ConfigurationService
|
|||
|
||||
public function getNamespace(): string
|
||||
{
|
||||
$coreClass = explode("\\", $this->getCore());
|
||||
$coreClass = explode('\\', $this->getCore());
|
||||
array_pop($coreClass);
|
||||
return implode("\\", $coreClass);
|
||||
$namespace = implode('\\', $coreClass);
|
||||
|
||||
return ltrim($namespace, '\\');
|
||||
}
|
||||
|
||||
public function getCore(): string
|
||||
|
|
@ -118,4 +83,46 @@ class ConfigurationService
|
|||
return $this->get('laminator/templates')
|
||||
?? ['Models', 'Services', 'Controllers', 'Endpoints', 'Routes'];
|
||||
}
|
||||
}
|
||||
|
||||
protected function setupDefines(): void
|
||||
{
|
||||
define('APP_ROOT', $this->appRoot);
|
||||
define('APP_NAME', $this->get('application/name'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate .benzine.yml.
|
||||
*
|
||||
* @param null|string $path
|
||||
*/
|
||||
protected function findConfig(string $path = null): bool
|
||||
{
|
||||
if (!$path) {
|
||||
$path = getcwd();
|
||||
//$path = dirname($this->environmentService->get('SCRIPT_FILENAME'));
|
||||
}
|
||||
if (!file_exists($path.'/.benzine.yml')) {
|
||||
$currentDirElem = explode(DIRECTORY_SEPARATOR, $path);
|
||||
array_pop($currentDirElem);
|
||||
$parentPath = implode(DIRECTORY_SEPARATOR, $currentDirElem);
|
||||
|
||||
return $this->findConfig($parentPath);
|
||||
}
|
||||
|
||||
$this->parseFile($path.'/.benzine.yml');
|
||||
$this->appRoot = $path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function parseFile(string $file)
|
||||
{
|
||||
$yaml = file_get_contents($file);
|
||||
foreach ($this->environmentService->all() as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$yaml = str_replace("\${$key}", $value, $yaml);
|
||||
}
|
||||
}
|
||||
$this->config = Yaml::parse($yaml);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,31 +12,39 @@ class EnvironmentService
|
|||
ksort($this->environmentVariables);
|
||||
}
|
||||
|
||||
public function has(string $key) : bool {
|
||||
return $this->get($key) !== null;
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return null !== $this->get($key);
|
||||
}
|
||||
|
||||
public function all() : array
|
||||
public function all(): array
|
||||
{
|
||||
ksort($this->environmentVariables);
|
||||
|
||||
return $this->environmentVariables;
|
||||
}
|
||||
|
||||
public function get(string $key, string $default = null){
|
||||
if(isset($this->environmentVariables[$key])){
|
||||
public function get(string $key, string $default = null)
|
||||
{
|
||||
if (isset($this->environmentVariables[$key])) {
|
||||
return $this->environmentVariables[$key];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function set(string $key, string $value) : self {
|
||||
public function set(string $key, string $value): self
|
||||
{
|
||||
$this->environmentVariables[$key] = $value;
|
||||
ksort($this->environmentVariables);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(string $key) : self {
|
||||
public function delete(string $key): self
|
||||
{
|
||||
unset($this->environmentVariables[$key]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ namespace Benzine\Services;
|
|||
class SessionService
|
||||
{
|
||||
protected \Redis $redis;
|
||||
|
||||
public function __construct(\Redis $redis)
|
||||
{
|
||||
$this->redis = $redis;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Benzine\Twig\Extensions;
|
||||
|
||||
use Benzine\Twig\Extensions\TransformExtensionException;
|
||||
use Camel\CaseTransformer;
|
||||
use Camel\Format;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace Benzine\Workers;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Benzine\Services\EnvironmentService;
|
||||
use Monolog\Logger;
|
||||
|
||||
abstract class AbstractQueueWorker extends AbstractWorker
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace Benzine\Workers;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Benzine\Services\EnvironmentService;
|
||||
use Monolog\Logger;
|
||||
|
||||
abstract class AbstractWorker
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue