Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extending Symfony DI in surf #800

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions bin/surf
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,31 @@
* file that was distributed with this source code.
*/

use Custom\MyExtension;
use SelfUpdate\SelfUpdateCommand;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use TYPO3\Surf\Cli\Symfony\ConsoleApplication;
use TYPO3\Surf\Cli\Symfony\ConsoleKernel;

requireAutoloader();


$kernel = new ConsoleKernel('prod');
$vendorPackages = array_map(
fn($dir) => json_decode(file_get_contents($dir . '/composer/installed.json'), true)['packages'] ?? [],
array_keys(\Composer\Autoload\ClassLoader::getRegisteredLoaders())
);
foreach ($vendorPackages as $packages) {
foreach ($packages as $package) {
if (
'typo3-surf-exstension' == $package['type']
&& null !== ($extension = $package['extra']['typo3-surf']['extension'] ?? null)
&& class_exists($extension)
) {
$kernel->addExtension(new $extension());
}
}
}

$kernel->boot();
$container = $kernel->getContainer();
/** @var ConsoleApplication $application */
Expand Down
19 changes: 18 additions & 1 deletion src/Cli/Symfony/ConsoleKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -26,19 +27,26 @@
use TYPO3\Surf\Cli\Symfony\CompilerPasses\CommandsToApplicationCompilerPass;
use TYPO3\Surf\Domain\Service\ShellCommandService;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

final class ConsoleKernel
{
private bool $booted = false;
private ?Container $container = null;
private string $environment;
private ?string $projectDir = null;
private array $extensions = [];

public function __construct(string $environment = 'dev')
{
$this->environment = $environment;
}

public function addExtension(ExtensionInterface $extension): void
{
$this->extensions[] = $extension;
}

private function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../../../Resources/services.php');
Expand Down Expand Up @@ -113,11 +121,20 @@ private function initializeContainer(): void
$container = new \ProjectServiceContainer();
} else {
$container = new ContainerBuilder();
$container->setParameter('kernel.environment', $this->environment);

$loader = new PhpFileLoader($container, new FileLocator());

$this->registerContainerConfiguration($loader);
$this->build($container);

foreach ($this->extensions as $extension) {
$container->registerExtension($extension);

$container->loadFromExtension($extension->getAlias(), null);
}
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass());

$this->build($container);
$container->compile();

$dumper = new PhpDumper($container);
Expand Down
81 changes: 81 additions & 0 deletions src/Cli/Symfony/DependencyInjection/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace TYPO3\Surf\Cli\Symfony\DependencyInjection;

use BadMethodCallException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

abstract class Extension implements ExtensionInterface
{
public function getXsdValidationBasePath()
{
return false;
}

public function getNamespace(): string
{
return 'http://example.org/schema/dic/' . $this->getAlias();
}

public function getAlias(): string
{
$className = static::class;
if (!str_ends_with($className, 'Extension')) {
throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
}
$classBaseName = substr(str_replace('\\', '', $className), 0, -9);

return Container::underscore($classBaseName);
}

public function loadExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
$services = $container->services();
$reflection = new \ReflectionClass($this);
$namespace = $reflection->getNamespaceName();
$services->defaults()
->autowire()
->autoconfigure()
->public();

$services->load($namespace . '\\', '*');
}

public function load(array $configs, ContainerBuilder $container): void
{
$reflection = new \ReflectionClass($this);
$file = $reflection->getFileName();
$dirname = dirname($file);

Check failure on line 62 in src/Cli/Symfony/DependencyInjection/Extension.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $path of function dirname expects string, string|false given.
$fileName = basename($file);

Check failure on line 63 in src/Cli/Symfony/DependencyInjection/Extension.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $path of function basename expects string, string|false given.

$env = $container->getParameter('kernel.environment');

$extensionLoader = new PhpFileLoader($container, new FileLocator());
$extensionLoader->setCurrentDir($dirname);

$instanceofClosure = &\Closure::bind(fn &() => $this->instanceof, $extensionLoader, $extensionLoader)();

(fn (ContainerConfigurator $configurator) => $this->loadExtension($configurator, $container))((new ContainerConfigurator(
$container,
$extensionLoader,
$instanceofClosure,
$dirname,
$fileName,
$env

Check failure on line 78 in src/Cli/Symfony/DependencyInjection/Extension.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #6 $env of class Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator constructor expects string|null, array|bool|float|int|string|UnitEnum|null given.
)));
}
}
Loading