Skip to content

Commit

Permalink
Upgrade to Symplify not using symfony/http-kernel (#1119)
Browse files Browse the repository at this point in the history
* trigger CI

* upgrade to symplify configs instead of bundles

* update config files infos to config files

* use ConfigureCallMerginLoaderFactory

* remove symfony/http-kernel

* remove symplify/phpstan-twig-rules
  • Loading branch information
TomasVotruba committed Nov 1, 2021
1 parent 1c27c6e commit 20291b2
Show file tree
Hide file tree
Showing 25 changed files with 256 additions and 272 deletions.
2 changes: 1 addition & 1 deletion bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\Console\Style\SymfonyStyleFactory;
use Rector\Core\DependencyInjection\RectorContainerFactory;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\Kernel\RectorKernel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
"rector/rector-doctrine": "^0.11.26",
"rector/rector-laravel": "^0.11.8",
"rector/rector-nette": "^0.11.31",
"rector/rector-phpoffice": "^0.11.4",
"rector/rector-phpunit": "^0.11.14",
"rector/rector-phpoffice": "^0.11.6",
"rector/rector-phpunit": "^0.11.16",
"rector/rector-symfony": "^0.11.31",
"sebastian/diff": "^4.0.4",
"ssch/typo3-rector": "^0.11.26",
"symfony/console": "^5.3.7",
"symfony/dependency-injection": "^5.3.7",
"symfony/finder": "^5.3.7",
"symfony/http-kernel": "^5.3.9",
"symfony/process": "^5.3.7",
"symfony/yaml": "^5.3.6",
"symplify/astral": "^9.5",
Expand Down Expand Up @@ -65,7 +64,6 @@
"symplify/easy-testing": "^9.5",
"symplify/monorepo-builder": "^9.5",
"symplify/phpstan-extensions": "^9.5",
"symplify/phpstan-twig-rules": "^9.5",
"symplify/phpstan-rules": "^9.5",
"symplify/rule-doc-generator": "^9.5"
},
Expand Down
2 changes: 1 addition & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
__DIR__ . '/../src/Exception',
__DIR__ . '/../src/DependencyInjection/CompilerPass',
__DIR__ . '/../src/DependencyInjection/Loader',
__DIR__ . '/../src/HttpKernel',
__DIR__ . '/../src/Kernel',
__DIR__ . '/../src/ValueObject',
__DIR__ . '/../src/Bootstrap',
__DIR__ . '/../src/Enum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\Core\Exception\ShouldNotHappenException;
use Symplify\SimplePhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;

final class UnionTypeNodePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor implements BasePhpDocNodeVisitorInterface
Expand All @@ -38,7 +37,7 @@ public function enterNode(Node $node): ?Node
// unwrap with parent array type...
$startAndEnd = $node->getAttribute(PhpDocAttributeKey::START_AND_END);
if (! $startAndEnd instanceof StartAndEnd) {
throw new ShouldNotHappenException();
return null;
}

$betterTokenProvider = $this->currentTokenIteratorProvider->provide();
Expand Down
22 changes: 11 additions & 11 deletions packages/Caching/Config/FileHashComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,51 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
*/
final class FileHashComputer
{
public function compute(SmartFileInfo $fileInfo): string
public function compute(string $filePath): string
{
$this->ensureIsPhp($fileInfo);
$this->ensureIsPhp($filePath);

$containerBuilder = new ContainerBuilder();
$fileLoader = $this->createFileLoader($fileInfo, $containerBuilder);

$fileLoader->load($fileInfo->getRealPath());
$fileLoader = $this->createFileLoader($filePath, $containerBuilder);
$fileLoader->load($filePath);

$parameterBag = $containerBuilder->getParameterBag();

return $this->arrayToHash($containerBuilder->getDefinitions()) . $this->arrayToHash($parameterBag->all());
}

private function ensureIsPhp(SmartFileInfo $fileInfo): void
private function ensureIsPhp(string $filePath): void
{
if ($fileInfo->hasSuffixes(['php'])) {
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
if ($fileExtension === 'php') {
return;
}

throw new ShouldNotHappenException(sprintf(
// getRealPath() cannot be used, as it breaks in phar
'Provide only PHP file, ready for Symfony Dependency Injection. "%s" given',
$fileInfo->getRelativeFilePath()
$filePath
));
}

private function createFileLoader(SmartFileInfo $fileInfo, ContainerBuilder $containerBuilder): LoaderInterface
private function createFileLoader(string $filePath, ContainerBuilder $containerBuilder): LoaderInterface
{
$fileLocator = new FileLocator([$fileInfo->getPath()]);
$fileLocator = new FileLocator([$filePath]);

$fileLoaders = [
new GlobFileLoader($containerBuilder, $fileLocator),
new PhpFileLoader($containerBuilder, $fileLocator),
];

$loaderResolver = new LoaderResolver($fileLoaders);
$loader = $loaderResolver->resolve($fileInfo->getRealPath());
$loader = $loaderResolver->resolve($filePath);
if (! $loader) {
throw new ShouldNotHappenException();
}
Expand Down
10 changes: 5 additions & 5 deletions packages/Caching/Detector/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public function getDependentFileInfos(SmartFileInfo $fileInfo): array
/**
* @api
*/
public function setFirstResolvedConfigFileInfo(SmartFileInfo $fileInfo): void
public function setFirstResolvedConfigFileInfo(string $filePath): void
{
// the first config is core to all → if it was changed, just invalidate it
$configHash = $this->fileHashComputer->compute($fileInfo);
$this->storeConfigurationDataHash($fileInfo, $configHash);
$configHash = $this->fileHashComputer->compute($filePath);
$this->storeConfigurationDataHash($filePath, $configHash);
}

private function getFileInfoCacheKey(SmartFileInfo $smartFileInfo): string
Expand All @@ -102,9 +102,9 @@ private function hashFile(SmartFileInfo $smartFileInfo): string
return (string) sha1_file($smartFileInfo->getRealPath());
}

private function storeConfigurationDataHash(SmartFileInfo $fileInfo, string $configurationHash): void
private function storeConfigurationDataHash(string $filePath, string $configurationHash): void
{
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($fileInfo->getRealPath());
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($filePath);
$this->invalidateCacheIfConfigurationChanged($key, $configurationHash);

$this->cache->save($key, CacheKey::CONFIGURATION_HASH_KEY, $configurationHash);
Expand Down
4 changes: 2 additions & 2 deletions packages/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected function setUp(): void
require_once __DIR__ . '/../../../preload.php';
}

$configFileInfo = new SmartFileInfo($this->provideConfigFilePath());
$this->bootFromConfigFileInfos([$configFileInfo]);
$configFile = $this->provideConfigFilePath();
$this->bootFromConfigFiles([$configFile]);

$this->applicationFileProcessor = $this->getService(ApplicationFileProcessor::class);
$this->parameterProvider = $this->getService(ParameterProvider::class);
Expand Down
31 changes: 17 additions & 14 deletions packages/Testing/PHPUnit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Rector\Testing\PHPUnit;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\HttpKernel\RectorKernel;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symplify\SmartFileSystem\SmartFileInfo;
use Rector\Core\Kernel\RectorKernel;
use Webmozart\Assert\Assert;

abstract class AbstractTestCase extends TestCase
{
Expand All @@ -21,25 +21,25 @@ abstract class AbstractTestCase extends TestCase

protected function boot(): void
{
$this->bootFromConfigFileInfos([]);
$this->bootFromConfigFiles([]);
}

/**
* @param SmartFileInfo[] $configFileInfos
* @param string[] $configFiles
*/
protected function bootFromConfigFileInfos(array $configFileInfos): void
protected function bootFromConfigFiles(array $configFiles): void
{
$configsHash = $this->createConfigsHash($configFileInfos);
$configsHash = $this->createConfigsHash($configFiles);

if (isset(self::$kernelsByHash[$configsHash])) {
$rectorKernel = self::$kernelsByHash[$configsHash];
self::$currentContainer = $rectorKernel->getContainer();
} else {
$rectorKernel = new RectorKernel('test_' . $configsHash, true, $configFileInfos);
$rectorKernel->boot();
$rectorKernel = new RectorKernel();
$container = $rectorKernel->createFromConfigs($configFiles);

self::$kernelsByHash[$configsHash] = $rectorKernel;
self::$currentContainer = $rectorKernel->getContainer();
self::$currentContainer = $container;
}
}

Expand All @@ -66,13 +66,16 @@ protected function getService(string $type): object
}

/**
* @param SmartFileInfo[] $configFileInfos
* @param string[] $configFiles
*/
private function createConfigsHash(array $configFileInfos): string
private function createConfigsHash(array $configFiles): string
{
Assert::allFile($configFiles);
Assert::allString($configFiles);

$configHash = '';
foreach ($configFileInfos as $configFileInfo) {
$configHash .= md5_file($configFileInfo->getRealPath());
foreach ($configFiles as $configFile) {
$configHash .= md5_file($configFile);
}

return $configHash;
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
<php>
<ini name="memory_limit" value="-1" />
<env name="XDEBUG_MODE" value="coverage"/>
<env name="KERNEL_CACHE_DIRECTORY" value="tmp/rector/cache"/>
</php>
</phpunit>
35 changes: 17 additions & 18 deletions src/Bootstrap/RectorConfigsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@
use Rector\Core\ValueObject\Bootstrap\BootstrapConfigs;
use Symfony\Component\Console\Input\ArgvInput;
use Symplify\SmartFileSystem\Exception\FileNotFoundException;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RectorConfigsResolver
{
public function provide(): BootstrapConfigs
{
$argvInput = new ArgvInput();
$mainConfigFileInfo = $this->resolveFromInputWithFallback($argvInput, 'rector.php');
$mainConfigFile = $this->resolveFromInputWithFallback($argvInput, 'rector.php');

$rectorRecipeConfigFileInfo = $this->resolveRectorRecipeConfig($argvInput);
$rectorRecipeConfigFile = $this->resolveRectorRecipeConfig($argvInput);

$configFileInfos = [];
if ($rectorRecipeConfigFileInfo !== null) {
$configFileInfos[] = $rectorRecipeConfigFileInfo;
$configFiles = [];
if ($rectorRecipeConfigFile !== null) {
$configFiles[] = $rectorRecipeConfigFile;
}

return new BootstrapConfigs($mainConfigFileInfo, $configFileInfos);
return new BootstrapConfigs($mainConfigFile, $configFiles);
}

private function resolveRectorRecipeConfig(ArgvInput $argvInput): ?SmartFileInfo
private function resolveRectorRecipeConfig(ArgvInput $argvInput): ?string
{
if ($argvInput->getFirstArgument() !== 'generate') {
return null;
Expand All @@ -38,25 +37,25 @@ private function resolveRectorRecipeConfig(ArgvInput $argvInput): ?SmartFileInfo
return null;
}

return new SmartFileInfo($rectorRecipeFilePath);
return $rectorRecipeFilePath;
}

private function resolveFromInput(ArgvInput $argvInput): ?SmartFileInfo
private function resolveFromInput(ArgvInput $argvInput): ?string
{
$configValue = $this->getOptionValue($argvInput, ['--config', '-c']);
if ($configValue === null) {
$configFile = $this->getOptionValue($argvInput, ['--config', '-c']);
if ($configFile === null) {
return null;
}

if (! file_exists($configValue)) {
$message = sprintf('File "%s" was not found', $configValue);
if (! file_exists($configFile)) {
$message = sprintf('File "%s" was not found', $configFile);
throw new FileNotFoundException($message);
}

return new SmartFileInfo($configValue);
return $configFile;
}

private function resolveFromInputWithFallback(ArgvInput $argvInput, string $fallbackFile): ?SmartFileInfo
private function resolveFromInputWithFallback(ArgvInput $argvInput, string $fallbackFile): ?string
{
$configFileInfo = $this->resolveFromInput($argvInput);
if ($configFileInfo !== null) {
Expand All @@ -66,14 +65,14 @@ private function resolveFromInputWithFallback(ArgvInput $argvInput, string $fall
return $this->createFallbackFileInfoIfFound($fallbackFile);
}

private function createFallbackFileInfoIfFound(string $fallbackFile): ?SmartFileInfo
private function createFallbackFileInfoIfFound(string $fallbackFile): ?string
{
$rootFallbackFile = getcwd() . DIRECTORY_SEPARATOR . $fallbackFile;
if (! is_file($rootFallbackFile)) {
return null;
}

return new SmartFileInfo($rootFallbackFile);
return $rootFallbackFile;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Config/Loader/ConfigureCallMergingLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Config\Loader;

use Rector\Core\DependencyInjection\Collector\ConfigureCallValuesCollector;
use Rector\Core\DependencyInjection\Loader\ConfigurableCallValuesCollectingPhpFileLoader;
use Symfony\Component\Config\FileLocator;
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 Symplify\SymfonyContainerBuilder\Contract\Config\LoaderFactoryInterface;

final class ConfigureCallMergingLoaderFactory implements LoaderFactoryInterface
{
public function __construct(
private ConfigureCallValuesCollector $configureCallValuesCollector
) {
}

public function create(ContainerBuilder $containerBuilder, string $currentWorkingDirectory): LoaderInterface
{
$fileLocator = new FileLocator([$currentWorkingDirectory]);

$loaderResolver = new LoaderResolver([
new GlobFileLoader($fileLocator),
new ConfigurableCallValuesCollectingPhpFileLoader(
$containerBuilder,
$fileLocator,
$this->configureCallValuesCollector
),
]);

return new DelegatingLoader($loaderResolver);
}
}
Loading

0 comments on commit 20291b2

Please sign in to comment.