Skip to content

Commit

Permalink
[FEATURE] Rewrite services to use more compiler passes, use more conc…
Browse files Browse the repository at this point in the history
…at_paths
  • Loading branch information
vertexvaar committed Oct 13, 2023
1 parent 8b91c4c commit 97da220
Show file tree
Hide file tree
Showing 23 changed files with 251 additions and 134 deletions.
3 changes: 2 additions & 1 deletion .ddev/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ database:
version: "10.3"
use_dns_when_possible: true
composer_version: ""
web_environment: []
web_environment:
- VXVR_BS_CONTEXT=Dev
nodejs_version: "16"

# Key features of DDEV's config.yaml:
Expand Down
3 changes: 0 additions & 3 deletions config/system.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php

use VerteXVaaR\BlueSprints\Utility\Context;

return [
'permissions' => [
'files' => 0660,
'folders' => 0770,
],
'context' => Context::CONTEXT_DEVELOPMENT,
];
8 changes: 5 additions & 3 deletions packages/bluecontainer/src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use VerteXVaaR\BlueContainer\DI;
use VerteXVaaR\BlueContainer\Helper\PackageIterator;
use VerteXVaaR\BlueSprints\Paths;
use VerteXVaaR\BlueSprints\Environment\Paths;

use function file_exists;
use function is_dir;
Expand Down Expand Up @@ -101,8 +101,10 @@ function (Package $package, string $installPath) use ($containerBuilder): void {
}
);

$pathsDefinition = $containerBuilder->getDefinition(Paths::class);
$configPath = $pathsDefinition->getArgument('$config');

$packageConfig = $this->composer->getPackage()->getConfig();
$configPath = $packageConfig['vertexvaar/bluesprints']['config'] ?? 'config';

if (file_exists($configPath . '/services.yaml')) {
$loader = new YamlFileLoader($containerBuilder, new FileLocator($configPath));
$loader->load('services.yaml');
Expand Down
12 changes: 6 additions & 6 deletions packages/bluefluid/src/Mvc/FluidTemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace VerteXVaaR\BlueFluid\Mvc;

use TYPO3Fluid\Fluid\View\TemplateView;
use VerteXVaaR\BlueSprints\Environment\Paths;
use VerteXVaaR\BlueSprints\Mvc\TemplateRenderer;
use VerteXVaaR\BlueSprints\Paths;

use const DIRECTORY_SEPARATOR as DS;
use function CoStack\Lib\concat_paths;

class FluidTemplateRenderer implements TemplateRenderer
{
Expand All @@ -24,11 +24,11 @@ public function __construct(private readonly Paths $paths)
public function render(string $templateName = ''): string
{
$controller = str_replace('\\', '/', $this->routeConfiguration['controller']);
$viewRootPath = VXVR_BS_ROOT . DS . $this->paths->view . DS;
$viewRootPath = concat_paths(VXVR_BS_ROOT, $this->paths->view);

$this->view->getTemplatePaths()->setTemplateRootPaths([$viewRootPath . 'Template/' . $controller . '/']);
$this->view->getTemplatePaths()->setLayoutRootPaths([$viewRootPath . 'Layout/' . $controller . '/']);
$this->view->getTemplatePaths()->setPartialRootPaths([$viewRootPath . 'Partial/' . $controller . '/']);
$this->view->getTemplatePaths()->setTemplateRootPaths([concat_paths($viewRootPath, 'Template', $controller)]);
$this->view->getTemplatePaths()->setLayoutRootPaths([concat_paths($viewRootPath, 'Layout', $controller)]);
$this->view->getTemplatePaths()->setPartialRootPaths([concat_paths($viewRootPath, 'Partial', $controller)]);
$this->view->getRenderingContext()->setControllerName(substr($controller, strrpos($controller, '/') + 1));

if (empty($templateName)) {
Expand Down
1 change: 1 addition & 0 deletions packages/bluesprints/.phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace PHPSTORM_META {
override(\Symfony\Component\DependencyInjection\Container::get(), type(0));
override(\VerteXVaaR\BlueSprints\Mvc\Repository::findByUuid(), type(0));
override(\VerteXVaaR\BlueSprints\Mvc\Repository::findAll(), map(['' => '@[]']));
}
5 changes: 3 additions & 2 deletions packages/bluesprints/bin/bluesprints
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

declare(strict_types=1);

use VerteXVaaR\BlueContainer\DI;
use VerteXVaaR\BlueSprints\Task\CliRequest;
use VerteXVaaR\BlueSprints\Task\Scheduler;

Expand All @@ -14,5 +15,5 @@ if (empty(ini_get('date.timezone'))) {
}

$cliRequest = CliRequest::createFromEnvironment();
$scheduler = new Scheduler($cliRequest);
$scheduler->run();
$di = new DI();
$di->get(Scheduler::class)->run($cliRequest);
3 changes: 2 additions & 1 deletion packages/bluesprints/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"psr/http-server-middleware": "^1.0",
"psr/http-server-handler": "^1.0",
"psr/container": "^2.0",
"guzzlehttp/psr7": "^2.6"
"guzzlehttp/psr7": "^2.6",
"co-stack/lib": "^v5.0.0"
},
"bin": [
"bin/bluesprints"
Expand Down
76 changes: 12 additions & 64 deletions packages/bluesprints/config/services.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
<?php

use Composer\Composer;
use Composer\Package\Package;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use VerteXVaaR\BlueContainer\Helper\PackageIterator;
use VerteXVaaR\BlueSprints\Config;
use VerteXVaaR\BlueSprints\Http\Server\Middleware\MiddlewareRegistry;
use VerteXVaaR\BlueSprints\Environment\DependencyInjection\ConfigCompilerPass;
use VerteXVaaR\BlueSprints\Environment\DependencyInjection\EnvironmentCompilerPass;
use VerteXVaaR\BlueSprints\Environment\DependencyInjection\PathsCompilerPass;
use VerteXVaaR\BlueSprints\Http\DependencyInjection\MiddlewareCompilerPass;
use VerteXVaaR\BlueSprints\Mvc\Controller;
use VerteXVaaR\BlueSprints\Mvc\DependencyInjection\PublicServicePass;
use VerteXVaaR\BlueSprints\Paths;
use VerteXVaaR\BlueSprints\Routing\DependencyInjection\RouteCollectorCompilerPass;

return static function (ContainerBuilder $containerBuilder): void {
/** @var Composer $composer */
$composer = $containerBuilder->get('composer');
return static function (ContainerBuilder $container): void {
$container->addCompilerPass(new EnvironmentCompilerPass());
$container->addCompilerPass(new PathsCompilerPass());
$container->addCompilerPass(new ConfigCompilerPass());
$container->addCompilerPass(new MiddlewareCompilerPass());
$container->addCompilerPass(new RouteCollectorCompilerPass());

$packageIterator = new PackageIterator($composer);
$middlewares = $packageIterator->iterate(static function (Package $package, string $installPath): array {
$middlewares = [];
if (file_exists($installPath . '/config/middlewares.php')) {
$packageMiddlewares = require $installPath . '/config/middlewares.php';
foreach ($packageMiddlewares as $packageMiddleware) {
$middlewares[] = new Reference($packageMiddleware['service']);
}
}
return $middlewares;
});

$packageConfig = $composer->getPackage()->getConfig();
$config = $packageConfig['vertexvaar/bluesprints'];
$pathsDefinition = $containerBuilder->getDefinition(Paths::class);
$pathsDefinition->setArguments([
'$logs' => $config['logs'] ?? 'var/logs',
'$locks' => $config['locks'] ?? 'var/locks',
'$cache' => $config['cache'] ?? 'var/cache',
'$database' => $config['database'] ?? 'var/database',
'$config' => $config['config'] ?? 'config',
'$view' => $config['view'] ?? 'view',
'$translations' => $config['translations'] ?? 'translations',
]);
$pathsDefinition->setShared(true);
$pathsDefinition->setPublic(true);

$systemSettings = [];
if (file_exists('config/system.php')) {
$systemSettings = require 'config/system.php';
}
$configDefinition = $containerBuilder->getDefinition(Config::class);
$configDefinition->setArguments([
'$filePermissions' => $config['permissions']['files'] ?? 0660,
'$folderPermissions' => $config['permissions']['folders'] ?? 0770,
]);
$configDefinition->setShared(true);
$configDefinition->setPublic(true);

$packageMiddlewares = [];
$middlewaresPath = $pathsDefinition->getArgument('$config') . '/middlewares.php';
if (file_exists($middlewaresPath)) {
$packageMiddlewares = require $middlewaresPath;
}

$middlewares = array_replace($packageMiddlewares, ...$middlewares);

$registry = $containerBuilder->getDefinition(MiddlewareRegistry::class);
$registry->setArgument('$middlewares', $middlewares);

$containerBuilder->addCompilerPass(new RouteCollectorCompilerPass());

$containerBuilder->registerForAutoconfiguration(Controller::class)
$container->registerForAutoconfiguration(Controller::class)
->addTag('vertexvaar.bluesprints.controller');
$containerBuilder->addCompilerPass(new PublicServicePass('vertexvaar.bluesprints.controller'));
$container->addCompilerPass(new PublicServicePass('vertexvaar.bluesprints.controller'));
};
15 changes: 15 additions & 0 deletions packages/bluesprints/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ services:
VerteXVaaR\BlueSprints\Http\HttpResponseEmitter:
public: true

VerteXVaaR\BlueSprints\Task\Scheduler:
public: true

VerteXVaaR\BlueSprints\Environment\Environment:
public: true
shared: true

VerteXVaaR\BlueSprints\Environment\Paths:
public: true
shared: true

VerteXVaaR\BlueSprints\Environment\Config:
public: true
shared: true

Psr\Http\Server\RequestHandlerInterface: '@VerteXVaaR\BlueSprints\Http\Server\RequestHandler\ControllerDispatcher'

VerteXVaaR\BlueSprints\Mvc\TemplateRenderer: '@VerteXVaaR\BlueSprints\Mvc\PhpTemplateRenderer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints;
namespace VerteXVaaR\BlueSprints\Environment;

readonly class Config
{
Expand Down
25 changes: 25 additions & 0 deletions packages/bluesprints/src/Environment/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints\Environment;

use JetBrains\PhpStorm\Pure;

use function str_starts_with;
use function strtolower;

enum Context
{
case Production;
case Development;

#[Pure]
public static function fromString(string $context): Context
{
if (str_starts_with(strtolower($context), 'dev')) {
return self::Development;
}
return self::Production;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints\Environment\DependencyInjection;

use Composer\Composer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use VerteXVaaR\BlueSprints\Environment\Config;

class ConfigCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
/** @var Composer $composer */
$composer = $container->get('composer');
$packageConfig = $composer->getPackage()->getConfig();
$permissions = $packageConfig['vertexvaar/bluesprints']['permissions'] ?? [];

$configDefinition = $container->getDefinition(Config::class);
$configDefinition->setArguments([
'$filePermissions' => $permissions['files'] ?? 0660,
'$folderPermissions' => $permissions['folders'] ?? 0770,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints\Environment\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use VerteXVaaR\BlueSprints\Environment\Context;
use VerteXVaaR\BlueSprints\Environment\Environment;

use function getenv;

class EnvironmentCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition(Environment::class);
$definition->setArgument('$context', Context::fromString((string)getenv('VXVR_BS_CONTEXT')));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints\Environment\DependencyInjection;

use Composer\Composer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use VerteXVaaR\BlueSprints\Environment\Paths;

class PathsCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
/** @var Composer $composer */
$composer = $container->get('composer');
$packageConfig = $composer->getPackage()->getConfig();
$config = $packageConfig['vertexvaar/bluesprints'];

$pathsDefinition = $container->getDefinition(Paths::class);
$pathsDefinition->setArguments([
'$logs' => $config['logs'] ?? 'var/logs',
'$locks' => $config['locks'] ?? 'var/locks',
'$cache' => $config['cache'] ?? 'var/cache',
'$database' => $config['database'] ?? 'var/database',
'$config' => $config['config'] ?? 'config',
'$view' => $config['view'] ?? 'view',
'$translations' => $config['translations'] ?? 'translations',
]);
}

}
13 changes: 13 additions & 0 deletions packages/bluesprints/src/Environment/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints\Environment;

final readonly class Environment
{
public function __construct(
public Context $context,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace VerteXVaaR\BlueSprints;
namespace VerteXVaaR\BlueSprints\Environment;

readonly class Paths
{
Expand Down
Loading

0 comments on commit 97da220

Please sign in to comment.