mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-25 04:03:55 +01:00
Merge pull request #419 from rectorphp/autoloader-parameters
Add autoload_files parameter
This commit is contained in:
commit
cabf3c3afe
16
README.md
16
README.md
@ -55,6 +55,18 @@ Rector relies on project and autoloading of its classes. To specify own autoload
|
||||
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
|
||||
```
|
||||
|
||||
Or make use of `rector.yml` config:
|
||||
|
||||
```yaml
|
||||
# rector.yml
|
||||
parameters:
|
||||
autoload_files:
|
||||
- '%kernel.project_dir%/vendor/squizlabs/php_codesniffer/autoload.php'
|
||||
|
||||
autoload_directories:
|
||||
- '%kernel.project_dir%/vendor/project-without-composer'
|
||||
```
|
||||
|
||||
## How to Reconstruct your Code
|
||||
|
||||
### A. Prepared Sets
|
||||
@ -138,10 +150,10 @@ This package has no intention in formatting your code, as **coding standard tool
|
||||
composer require --dev symplify/easy-coding-standard
|
||||
|
||||
# check
|
||||
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon
|
||||
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.yml
|
||||
|
||||
# fix
|
||||
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.neon --fix
|
||||
vendor/bin/ecs check --config vendor/rector/rector/ecs-after-rector.yml --fix
|
||||
```
|
||||
|
||||
## More Detailed Documentation
|
||||
|
@ -4,6 +4,7 @@ namespace Rector\Autoloading;
|
||||
|
||||
use Nette\Loaders\RobotLoader;
|
||||
use Rector\Configuration\Option;
|
||||
use Rector\FileSystem\FileGuard;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
|
||||
@ -14,14 +15,25 @@ final class AdditionalAutoloader
|
||||
*/
|
||||
private const AUTOLOAD_DIRECTORIES_PARAMETER = 'autoload_directories';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const AUTOLOAD_FILES_PARAMETER = 'autoload_files';
|
||||
|
||||
/**
|
||||
* @var ParameterProvider
|
||||
*/
|
||||
private $parameterProvider;
|
||||
|
||||
public function __construct(ParameterProvider $parameterProvider)
|
||||
/**
|
||||
* @var FileGuard
|
||||
*/
|
||||
private $fileGuard;
|
||||
|
||||
public function __construct(ParameterProvider $parameterProvider, FileGuard $fileGuard)
|
||||
{
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
$this->fileGuard = $fileGuard;
|
||||
}
|
||||
|
||||
public function autoloadWithInput(InputInterface $input): void
|
||||
@ -38,29 +50,48 @@ final class AdditionalAutoloader
|
||||
return;
|
||||
}
|
||||
|
||||
if (! is_file($autoloadFile) || ! file_exists($autoloadFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
require_once $autoloadFile;
|
||||
$this->autoloadFiles([$autoloadFile]);
|
||||
}
|
||||
|
||||
private function autoloadDirectoriesFromParameter(ParameterProvider $parameterProvider): void
|
||||
{
|
||||
$autoloadDirectories = $parameterProvider->provideParameter(self::AUTOLOAD_DIRECTORIES_PARAMETER);
|
||||
if ($autoloadDirectories === null) {
|
||||
return;
|
||||
if ($autoloadDirectories !== null) {
|
||||
$this->autoloadDirectories($autoloadDirectories);
|
||||
}
|
||||
|
||||
$autoloadFiles = $parameterProvider->provideParameter(self::AUTOLOAD_FILES_PARAMETER);
|
||||
if ($autoloadFiles !== null) {
|
||||
$this->autoloadFiles($autoloadFiles);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $directories
|
||||
*/
|
||||
private function autoloadDirectories(array $directories): void
|
||||
{
|
||||
$robotLoader = new RobotLoader();
|
||||
$robotLoader->ignoreDirs = ['*Fixtures'] + $robotLoader->ignoreDirs;
|
||||
// last argument is workaround: https://github.com/nette/robot-loader/issues/12
|
||||
$robotLoader->setTempDirectory(__DIR__ . '/../../temp/_rector_robot_loader');
|
||||
|
||||
foreach ($autoloadDirectories as $autoloadDirectory) {
|
||||
foreach ($directories as $autoloadDirectory) {
|
||||
$robotLoader->addDirectory($autoloadDirectory);
|
||||
}
|
||||
|
||||
$robotLoader->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $files
|
||||
*/
|
||||
private function autoloadFiles(array $files): void
|
||||
{
|
||||
foreach ($files as $file) {
|
||||
$this->fileGuard->ensureFileExists($file, 'Extra autoload');
|
||||
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ use Rector\Exception\FileSystem\FileNotFoundException;
|
||||
|
||||
final class FileGuard
|
||||
{
|
||||
public static function ensureFileExists(string $file, string $location): void
|
||||
public function ensureFileExists(string $file, string $location): void
|
||||
{
|
||||
if (file_exists($file)) {
|
||||
if (is_file($file) && file_exists($file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -27,10 +27,16 @@ abstract class AbstractRectorTestCase extends TestCase
|
||||
*/
|
||||
protected $parameterProvider;
|
||||
|
||||
/**
|
||||
* @var FileGuard
|
||||
*/
|
||||
private $fileGuard;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$config = $this->provideConfig();
|
||||
FileGuard::ensureFileExists($config, get_called_class());
|
||||
$this->fileGuard = new FileGuard();
|
||||
$this->fileGuard->ensureFileExists($config, get_called_class());
|
||||
|
||||
$this->container = (new ContainerFactory())->createWithConfig($config);
|
||||
$this->fileProcessor = $this->container->get(FileProcessor::class);
|
||||
@ -39,8 +45,8 @@ abstract class AbstractRectorTestCase extends TestCase
|
||||
|
||||
protected function doTestFileMatchesExpectedContent(string $file, string $reconstructedFile): void
|
||||
{
|
||||
FileGuard::ensureFileExists($file, get_called_class());
|
||||
FileGuard::ensureFileExists($reconstructedFile, get_called_class());
|
||||
$this->fileGuard->ensureFileExists($file, get_called_class());
|
||||
$this->fileGuard->ensureFileExists($reconstructedFile, get_called_class());
|
||||
|
||||
$this->parameterProvider->changeParameter('source', [$file]);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user