Skip to content
Merged
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
12 changes: 4 additions & 8 deletions bin/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ function includeProjectsAutoload(string $composerJsonPath, string $cwd): void

$composerSettings = json_decode($contents, true);
if (! is_array($composerSettings)) {
fwrite(STDERR, "Failed to load '${composerJsonPath}'\n");
exit(1);
die(sprintf('Failed to load "%s"', $composerJsonPath));
}

$vendorPath = $composerSettings['config']['vendor-dir'] ?? $cwd . '/vendor';
if (! is_dir($vendorPath)) {
fwrite(STDERR, "Please check if 'composer.phar install' was run already (expected to find '${vendorPath}')\n");
exit(1);
die(sprintf('Please check if "composer install" was run already (expected to find "%s")', $vendorPath));
}

/** @noinspection PhpIncludeInspection */
require $vendorPath . '/autoload.php';
}

Expand All @@ -29,6 +26,7 @@ function includeProjectsAutoload(string $composerJsonPath, string $cwd): void
require $projectAutoload;
}

// is autolaod successful?
if (class_exists('Rector\HttpKernel\RectorKernel')) {
return;
}
Expand All @@ -53,11 +51,9 @@ function includeProjectsAutoload(string $composerJsonPath, string $cwd): void
return;
}

fwrite(
STDERR,
die(
sprintf(
'Composer autoload.php was not found in paths "%s". Have you ran "composer update"?',
implode('", "', $possibleAutoloadPaths)
)
);
exit(1);
31 changes: 11 additions & 20 deletions bin/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
declare(strict_types=1);

use Rector\Console\Option\SetOptionResolver;
use Rector\DependencyInjection\RectorContainerFactory;
use Rector\Exception\Configuration\SetNotFoundException;
use Rector\HttpKernel\RectorKernel;
use Rector\Set\Set;
use Symfony\Component\Console\Input\ArgvInput;
use Symplify\PackageBuilder\Configuration\ConfigFileFinder;
use Symplify\PackageBuilder\Console\Input\InputDetector;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;

$configFiles = [];
$configs = [];

// Detect configuration from --set
try {
$configFiles[] = (new SetOptionResolver())->detectFromInputAndDirectory(
new ArgvInput(),
__DIR__ . '/../config/set'
);
$input = new ArgvInput();
$setOptionResolver = new SetOptionResolver();
$configs[] = $setOptionResolver->detectFromInputAndDirectory($input, Set::SET_DIRECTORY);
} catch (SetNotFoundException $setNotFoundException) {
$symfonyStyle = (new SymfonyStyleFactory())->create();
$symfonyStyle->error($setNotFoundException->getMessage());
Expand All @@ -27,19 +26,11 @@

// And from --config or default one
ConfigFileFinder::detectFromInput('rector', new ArgvInput());
$configFiles[] = ConfigFileFinder::provide('rector', ['rector.yml', 'rector.yaml']);
$configs[] = ConfigFileFinder::provide('rector', ['rector.yml', 'rector.yaml']);

// remove empty values
$configFiles = array_filter($configFiles);
$configs = array_filter($configs);

// 3. Build DI container

// to override the configs without clearing cache
$environment = 'prod' . random_int(1, 10000000);
$rectorKernel = new RectorKernel($environment, InputDetector::isDebug());
if ($configFiles) {
$rectorKernel->setConfigs($configFiles);
}
$rectorKernel->boot();

return $rectorKernel->getContainer();
// Build DI container
$rectorContainerFactory = new RectorContainerFactory();
return $rectorContainerFactory->createFromConfigs($configs);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"bin": ["bin/rector"],
"require": {
"php": "^7.1",
"ext-json": "*",
"composer/xdebug-handler": "^1.3",
"doctrine/annotations": "^1.6",
"doctrine/inflector": "^1.3",
Expand Down
2 changes: 1 addition & 1 deletion ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ parameters:
- 'packages/BetterPhpDocParser/src/Annotation/AnnotationNaming.php'
- 'src/Testing/PHPUnit/PHPUnitEnvironment.php'
# honesty first
- 'src/*StaticHelper.php'
- 'src/*Static*.php'

Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
- 'packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php'
Expand Down
41 changes: 21 additions & 20 deletions src/Console/Option/SetOptionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function detectFromInputAndDirectory(InputInterface $input, string $confi
return null;
}

return $this->detectFromNameAndDirectory($setName, $configDirectory);
}

public function detectFromNameAndDirectory(string $setName, string $configDirectory): ?string
{
$nearestMatches = $this->findNearestMatchingFiles($configDirectory, $setName);
if (count($nearestMatches) === 0) {
$this->reportSetNotFound($configDirectory, $setName);
Expand All @@ -57,18 +62,16 @@ private function reportSetNotFound(string $configDirectory, string $setName): vo

$suggestedSet = ObjectHelpers::getSuggestion($allSets, $setName);

$hasSetVersion = (bool) Strings::match($setName, '#[\d]#');

[$versionedSets, $unversionedSets] = $this->separateVersionedAndUnversionedSets($allSets);

$setsListInString = $this->createSetListInString($hasSetVersion, $unversionedSets, $versionedSets);
$setsListInString = $this->createSetListInString($unversionedSets, $versionedSets);

$setNotFoundMessage = sprintf(
'%s "%s" was not found.%s%s',
ucfirst($this->keyName),
$setName,
PHP_EOL,
$suggestedSet ? sprintf('Did you mean "%s"?', $suggestedSet) . PHP_EOL : 'Pick one of above.'
$suggestedSet ? sprintf('Did you mean "%s"?', $suggestedSet) . PHP_EOL : ''
);

$pickOneOfMessage = sprintf('Pick "--%s" of:%s%s', $this->keyName, PHP_EOL . PHP_EOL, $setsListInString);
Expand Down Expand Up @@ -152,24 +155,18 @@ private function matchVersionInTheEnd(string $setName): ?string
* @param string[] $unversionedSets
* @param string[] $versionedSets
*/
private function createSetListInString(
bool $hasSetVersion,
array $unversionedSets,
array $versionedSets
): string {
private function createSetListInString(array $unversionedSets, array $versionedSets): string
{
$setsListInString = '';

if ($hasSetVersion === false) {
foreach ($unversionedSets as $unversionedSet) {
$setsListInString .= ' * ' . $unversionedSet . PHP_EOL;
}
foreach ($unversionedSets as $unversionedSet) {
$setsListInString .= ' * ' . $unversionedSet . PHP_EOL;
}

if ($hasSetVersion) {
foreach ($versionedSets as $groupName => $configName) {
$setsListInString .= ' * ' . $groupName . ': ' . implode(', ', $configName) . PHP_EOL;
}
foreach ($versionedSets as $groupName => $configName) {
$setsListInString .= ' * ' . $groupName . ': ' . implode(', ', $configName) . PHP_EOL;
}

return $setsListInString;
}

Expand All @@ -183,12 +180,16 @@ private function separateVersionedAndUnversionedSets(array $allSets): array
$unversionedSets = [];

foreach ($allSets as $set) {
$match = Strings::match($set, '#^[A-Za-z\-]+#');
if ($match === null) {
$hasVersion = (bool) Strings::match($set, '#\d#');

if ($hasVersion === false) {
$unversionedSets[] = $set;
continue;
}

$setWithoutVersion = rtrim($match[0], '-');
$match = Strings::match($set, '#^(?<set>[A-Za-z\-]+)#');
$setWithoutVersion = $match['set'];

if ($setWithoutVersion !== $set) {
$versionedSets[$setWithoutVersion][] = $set;
}
Expand Down
72 changes: 72 additions & 0 deletions src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Rector\DependencyInjection;

use Psr\Container\ContainerInterface;
use Rector\Console\Option\SetOptionResolver;
use Rector\Exception\ShouldNotHappenException;
use Rector\HttpKernel\RectorKernel;
use Rector\Set\Set;
use Symplify\PackageBuilder\Configuration\ConfigFileFinder;
use Symplify\PackageBuilder\Console\Input\InputDetector;

final class RectorContainerFactory
{
/**
* @var SetOptionResolver
*/
private $setOptionResolver;

public function __construct()
{
$this->setOptionResolver = new SetOptionResolver();
}

public function createFromSet(string $set): ContainerInterface
{
$configFiles = $this->resolveConfigs($set);

return $this->createFromConfigs($configFiles);
}

/**
* @param string[] $configFiles
*/
public function createFromConfigs(array $configFiles): ContainerInterface
{
// to override the configs without clearing cache
$environment = 'prod' . random_int(1, 10000000);
$isDebug = InputDetector::isDebug();

$rectorKernel = new RectorKernel($environment, $isDebug);
if ($configFiles) {
$rectorKernel->setConfigs($configFiles);
}

$rectorKernel->boot();

return $rectorKernel->getContainer();
}

/**
* @return string[]
*/
private function resolveConfigs(string $set): array
{
$config = $this->setOptionResolver->detectFromNameAndDirectory($set, Set::SET_DIRECTORY);
if ($config === null) {
throw new ShouldNotHappenException(sprintf('Config file for "%s" set was not found', $set));
}

// copied mostly from https://github.com/rectorphp/rector/blob/master/bin/container.php
$configFiles = [];
$configFiles[] = $config;
// local config has priority
$configFiles[] = ConfigFileFinder::provide('rector', ['rector.yml', 'rector.yaml']);

// remove empty values
return array_filter($configFiles);
}
}
13 changes: 13 additions & 0 deletions src/Set/Set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Set;

final class Set
{
/**
* @var string
*/
public const SET_DIRECTORY = __DIR__ . '/../../config/set';
}
2 changes: 1 addition & 1 deletion src/Set/SetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SetProvider
public function provide(): array
{
$finder = Finder::create()->files()
->in(__DIR__ . '/../../config/set');
->in(Set::SET_DIRECTORY);

$sets = [];
foreach ($finder->getIterator() as $fileInfo) {
Expand Down
Loading