-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Add custom container and kernel (#1610)
Resolves: #1609
- Loading branch information
1 parent
9d70de2
commit 591ba88
Showing
6 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\DependencyInjection; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Rector\Core\Stubs\StubLoader; | ||
use Ssch\TYPO3Rector\HttpKernel\Typo3RectorKernel; | ||
use Symplify\PackageBuilder\Console\Input\InputDetector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class Typo3RectorContainerFactory | ||
{ | ||
/** | ||
* @param SmartFileInfo[] $configFileInfos | ||
* @api | ||
*/ | ||
public function createFromConfigs(array $configFileInfos): ContainerInterface | ||
{ | ||
// to override the configs without clearing cache | ||
$environment = 'prod' . random_int(1, 10000000); | ||
$isDebug = InputDetector::isDebug(); | ||
|
||
$rectorKernel = new Typo3RectorKernel($environment, $isDebug); | ||
if ([] !== $configFileInfos) { | ||
$configFilePaths = $this->unpackRealPathsFromFileInfos($configFileInfos); | ||
$rectorKernel->setConfigs($configFilePaths); | ||
} | ||
|
||
$stubLoader = new StubLoader(); | ||
$stubLoader->loadStubs(); | ||
|
||
$rectorKernel->boot(); | ||
|
||
return $rectorKernel->getContainer(); | ||
} | ||
|
||
/** | ||
* @param SmartFileInfo[] $configFileInfos | ||
* @return string[] | ||
*/ | ||
private function unpackRealPathsFromFileInfos(array $configFileInfos): array | ||
{ | ||
$configFilePaths = []; | ||
foreach ($configFileInfos as $configFileInfo) { | ||
// getRealPath() cannot be used, as it breaks in phar | ||
$configFilePaths[] = $configFileInfo->getRealPath() ?: $configFileInfo->getPathname(); | ||
} | ||
|
||
return $configFilePaths; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\HttpKernel; | ||
|
||
use Migrify\PhpConfigPrinter\Bundle\PhpConfigPrinterBundle; | ||
use Rector\Core\Contract\Rector\RectorInterface; | ||
use Rector\Core\DependencyInjection\Collector\ConfigureCallValuesCollector; | ||
use Rector\Core\DependencyInjection\CompilerPass\MakeRectorsPublicCompilerPass; | ||
use Rector\Core\DependencyInjection\CompilerPass\MergeImportedRectorConfigureCallValuesCompilerPass; | ||
use Rector\Core\DependencyInjection\Loader\ConfigurableCallValuesCollectingPhpFileLoader; | ||
use Symfony\Component\Config\Loader\DelegatingLoader; | ||
use Symfony\Component\Config\Loader\GlobFileLoader; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\Config\Loader\LoaderResolver; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\Bundle\BundleInterface; | ||
use Symfony\Component\HttpKernel\Config\FileLocator; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Symplify\AutowireArrayParameter\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass; | ||
use Symplify\ComposerJsonManipulator\ComposerJsonManipulatorBundle; | ||
use Symplify\ConsoleColorDiff\ConsoleColorDiffBundle; | ||
use Symplify\PackageBuilder\Contract\HttpKernel\ExtraConfigAwareKernelInterface; | ||
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireInterfacesCompilerPass; | ||
|
||
final class Typo3RectorKernel extends Kernel implements ExtraConfigAwareKernelInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $configs = []; | ||
|
||
/** | ||
* @var ConfigureCallValuesCollector | ||
*/ | ||
private $configureCallValuesCollector; | ||
|
||
public function __construct(string $environment, bool $debug) | ||
{ | ||
$this->configureCallValuesCollector = new ConfigureCallValuesCollector(); | ||
|
||
parent::__construct($environment, $debug); | ||
} | ||
|
||
public function getCacheDir(): string | ||
{ | ||
// manually configured, so it can be replaced in phar | ||
return sys_get_temp_dir() . '/_rector'; | ||
} | ||
|
||
public function getLogDir(): string | ||
{ | ||
// manually configured, so it can be replaced in phar | ||
return sys_get_temp_dir() . '/_rector_log'; | ||
} | ||
|
||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
$loader->load(__DIR__ . '/../../vendor/rector/rector/config/config.php'); | ||
$loader->load(__DIR__ . '/../../config/services.php'); | ||
|
||
foreach ($this->configs as $config) { | ||
$loader->load($config); | ||
} | ||
} | ||
|
||
/** | ||
* @param string[] $configs | ||
*/ | ||
public function setConfigs(array $configs): void | ||
{ | ||
$this->configs = $configs; | ||
} | ||
|
||
/** | ||
* @return BundleInterface[] | ||
*/ | ||
public function registerBundles(): array | ||
{ | ||
return [new ConsoleColorDiffBundle(), new PhpConfigPrinterBundle(), new ComposerJsonManipulatorBundle()]; | ||
} | ||
|
||
protected function build(ContainerBuilder $containerBuilder): void | ||
{ | ||
$containerBuilder->addCompilerPass(new AutowireArrayParameterCompilerPass()); | ||
|
||
// autowire Rectors by default (mainly for 3rd party code) | ||
$containerBuilder->addCompilerPass(new AutowireInterfacesCompilerPass([RectorInterface::class])); | ||
|
||
$containerBuilder->addCompilerPass(new MakeRectorsPublicCompilerPass()); | ||
|
||
// add all merged arguments of Rector services | ||
$containerBuilder->addCompilerPass( | ||
new MergeImportedRectorConfigureCallValuesCompilerPass($this->configureCallValuesCollector) | ||
); | ||
} | ||
|
||
/** | ||
* This allows to use "%vendor%" variables in imports | ||
*/ | ||
protected function getContainerLoader(ContainerInterface $container): DelegatingLoader | ||
{ | ||
$fileLocator = new FileLocator($this); | ||
|
||
$loaders = [new GlobFileLoader($fileLocator)]; | ||
|
||
if ($container instanceof ContainerBuilder) { | ||
$loaders[] = new ConfigurableCallValuesCollectingPhpFileLoader( | ||
$container, | ||
$fileLocator, | ||
$this->configureCallValuesCollector | ||
); | ||
} | ||
|
||
$loaderResolver = new LoaderResolver($loaders); | ||
|
||
return new DelegatingLoader($loaderResolver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters