Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[ECS] Make phar ready by putting value objects to own namespace and e…
Browse files Browse the repository at this point in the history
…xclude dir
  • Loading branch information
TomasVotruba committed Jan 9, 2020
1 parent aeca4d1 commit 6fbc65a
Show file tree
Hide file tree
Showing 30 changed files with 187 additions and 79 deletions.
117 changes: 97 additions & 20 deletions packages/EasyCodingStandard/bin/ecs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,10 @@ use Symplify\EasyCodingStandard\Configuration\Configuration;
gc_disable();

# 1. autoload
$possibleAutoloadPaths = [
// after split package
__DIR__ . '/../vendor',
// dependency
__DIR__ . '/../../..',
// monorepo
__DIR__ . '/../../../vendor',
];

foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (is_file($possibleAutoloadPath . '/autoload.php')) {
require_once $possibleAutoloadPath . '/autoload.php';
require_once $possibleAutoloadPath . '/squizlabs/php_codesniffer/autoload.php';

// initalize PHPCS tokens
new Tokens();

break;
}
}
$autoloadIncluder = new AutoloadIncluder();
$autoloadIncluder->includeCwdVendorAutoloadIfExists();
$autoloadIncluder->autoloadProjectAutoloaderFile('/../../autoload.php');
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();

# 2. create container
$configs = [];
Expand Down Expand Up @@ -112,3 +96,96 @@ $configuration->setFirstResolverConfig($configResolver->getFirstResolvedConfig()
# 3. run
$application = $container->get(EasyCodingStandardConsoleApplication::class);
exit($application->run());


/**
* Inspired by https://github.com/rectorphp/rector/pull/2373/files#diff-0fc04a2bb7928cac4ae339d5a8bf67f3
*/
final class AutoloadIncluder
{
/**
* @var string[]
*/
private $alreadyLoadedAutoloadFiles = [];

public function includeCwdVendorAutoloadIfExists(): void
{
$cwdVendorAutoload = getcwd() . '/vendor/autoload.php';
if (!is_file($cwdVendorAutoload)) {
return;
}
$this->loadIfNotLoadedYet($cwdVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}

public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void
{
// ECS' vendor is already loaded
if (class_exists('\Symplify\EasyCodingStandard\HttpKernel\EasyCodingStandardKernel')) {
return;
}

$devOrPharVendorAutoload = __DIR__ . '/../vendor/autoload.php';
if (! is_file($devOrPharVendorAutoload)) {
return;
}

$this->loadIfNotLoadedYet($devOrPharVendorAutoload, __METHOD__ . '()" on line ' . __LINE__);
}
/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/e2308ecaf49a9960510c47f5a992ce7b27f6dba2/bin/phpstan#L19
*/
public function autoloadProjectAutoloaderFile(string $file): void
{
$path = dirname(__DIR__) . $file;
if (!extension_loaded('phar')) {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
} else {
$pharPath = Phar::running(false);
if ($pharPath === '') {
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
} else {
$path = dirname($pharPath) . $file;
if (is_file($path)) {
$this->loadIfNotLoadedYet($path, __METHOD__ . '()" on line ' . __LINE__);
}
}
}
}

private function loadIfNotLoadedYet(string $file, string $location): void
{
if (in_array($file, $this->alreadyLoadedAutoloadFiles, true)) {
return;
}

if ($this->isDebugOption()) {
echo sprintf(sprintf(
'File "%s" is about to be loaded in "%s"' . PHP_EOL,
$file,
$location
));
}

$this->alreadyLoadedAutoloadFiles[] = realpath($file);
require_once $file;

$phpCodeSnifferAutoload = dirname($file) . '/squizlabs/php_codesniffer/autoload.php';
if (!file_exists($phpCodeSnifferAutoload)) {
return;
}

require_once $phpCodeSnifferAutoload;

// initalize PHPCS tokens
new Tokens();
}

private function isDebugOption(): bool
{
return in_array('--debug', $_SERVER['argv'], true);
}
}
7 changes: 6 additions & 1 deletion packages/EasyCodingStandard/compiler/build/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ function (string $filePath, string $prefix, string $content): string {
return str_replace('__DIR__ . \'/..', '\'phar://ecs.phar', $content);
},
],
'whitelist' => ['Symplify\*', 'PHP_CodeSniffer\*', 'PhpCsFixer\*'],
'whitelist' => [
'Symplify\EasyCodingStandard\*',
'Symplify\CodingStandard\*',
'PHP_CodeSniffer\*',
'PhpCsFixer\*',
],
];
1 change: 0 additions & 1 deletion packages/EasyCodingStandard/compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"symfony/console": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symfony/polyfill-php73": "^1.13",
"symfony/process": "^4.4|^5.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\EasyCodingStandard\Compiler\Exception\ShouldNotHappenException;
use Symplify\EasyCodingStandard\Compiler\Process\CompileProcessFactory;

/**
Expand All @@ -35,7 +36,7 @@ final class CompileCommand extends Command
private $originalComposerJsonFileContent;

/**
* @var string
* @var string|null
*/
private $symplifyVersionToRequire;

Expand Down Expand Up @@ -77,6 +78,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->buildDir
);

// remove bugging packages
$dirsToRemove = [__DIR__ . '/../../vendor/symfony/polyfill-php70'];
foreach ($dirsToRemove as $dirToRemove) {
NetteFileSystem::delete($dirToRemove);
}

// parallel prevention is just for single less-buggy process
$this->compileProcessFactory->create(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir);

Expand All @@ -98,6 +105,11 @@ private function fixComposerJson(string $composerJsonFile): void
// simplify autoload (remove not packed build directory]
$json['autoload']['psr-4']['Symplify\\EasyCodingStandard\\'] = 'src';

// remove dev content
unset($json['minimum-stability']);
unset($json['prefer-stable']);
unset($json['extra']);

// use stable version for symplify packages
foreach (array_keys($json['require']) as $package) {
/** @var string $package */
Expand All @@ -109,6 +121,15 @@ private function fixComposerJson(string $composerJsonFile): void
$json['require'][$package] = $symplifyVersionToRequire;
}

// cleanup
$filesToRemove = [
__DIR__ . '/../../../vendor/friendsofphp/php-cs-fixer/src/Test/AbstractIntegrationTestCase.php',
];

foreach ($filesToRemove as $fileToRemove) {
NetteFileSystem::delete($fileToRemove);
}

$encodedJson = Json::encode($json, Json::PRETTY);

$this->filesystem->dumpFile($composerJsonFile, $encodedJson);
Expand All @@ -126,14 +147,20 @@ private function restoreComposerJson(string $composerJsonFile): void

private function getSymplifyStableVersionToRequire(): string
{
if ($this->symplifyVersionToRequire) {
if ($this->symplifyVersionToRequire !== null) {
return $this->symplifyVersionToRequire;
}

$symplifyPackageContent = NetteFileSystem::read('https://repo.packagist.org/p/symplify/symplify.json');
$symplifyPackageContent = file_get_contents('https://repo.packagist.org/p/symplify/symplify.json');
if ($symplifyPackageContent === null) {
throw new ShouldNotHappenException();
}

$symplifyPackageJson = Json::decode($symplifyPackageContent, Json::FORCE_ARRAY);
$symplifyPackageVersions = $symplifyPackageJson['packages']['symplify/symplify'];
end($symplifyPackageVersions);

$lastStableVersion = array_key_last($symplifyPackageJson['packages']['symplify/symplify']);
$lastStableVersion = key($symplifyPackageVersions);
$lastStableVersion = new Version($lastStableVersion);

$this->symplifyVersionToRequire = '^' . $lastStableVersion->getMajor()->getValue() . '.' . $lastStableVersion->getMinor()->getValue();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Compiler\Exception;

use Exception;

final class ShouldNotHappenException extends Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symplify\EasyCodingStandard\Compiler\Contract\Process\ProcessInterface;

final class CompileProcessFactory
{
Expand All @@ -23,7 +22,7 @@ public function __construct()
/**
* @param string[] $command
*/
public function create(array $command, string $cwd): ProcessInterface
public function create(array $command, string $cwd): SymfonyProcess
{
return new SymfonyProcess($command, $cwd, $this->output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,17 @@

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symplify\EasyCodingStandard\Compiler\Contract\Process\ProcessInterface;

final class SymfonyProcess implements ProcessInterface
final class SymfonyProcess
{
/**
* @var Process
*/
private $process;

/**
* @param string[] $command
*/
public function __construct(array $command, string $cwd, OutputInterface $output)
{
$this->process = (new Process($command, $cwd, null, null, null))
(new Process($command, $cwd, null, null, null))
->mustRun(static function (string $type, string $buffer) use ($output): void {
$output->write($buffer);
});
}

public function getProcess(): Process
{
return $this->process;
}
}
13 changes: 6 additions & 7 deletions packages/EasyCodingStandard/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ services:
Symplify\EasyCodingStandard\:
resource: '../src'
exclude:
- '../src/Contract'
- '../src/DependencyInjection'
- '../src/HttpKernel'
- '../src/Exception'
- '../src/Error/Error.php'
- '../src/Error/FileDiff.php'
- '../src/Yaml'
- '../src/Contract/*'
- '../src/DependencyInjection/*'
- '../src/HttpKernel/*'
- '../src/Exception/*'
- '../src/ValueObject/*'
- '../src/Yaml/*'

_instanceof:
Symplify\EasyCodingStandard\Contract\Application\FileProcessorCollectorInterface:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
Symplify\EasyCodingStandard\Configuration\:
resource: '../src'
exclude:
- '../src/Exception'
- '../src/Exception/*'
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
Symplify\EasyCodingStandard\FixerRunner\:
resource: '../src'
exclude:
- '../src/Exception'
- '../src/Exception/*'

# differ
PhpCsFixer\Differ\UnifiedDiffer: ~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ services:
Symplify\EasyCodingStandard\SniffRunner\:
resource: '../src'
exclude:
- '../src/Exception'
- '../src/File/File.php'
- '../src/Exception/*'
- '../src/ValueObject/*'
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Symplify\EasyCodingStandard\Contract\Application\DualRunInterface;
use Symplify\EasyCodingStandard\Contract\Application\FileProcessorInterface;
use Symplify\EasyCodingStandard\Error\ErrorAndDiffCollector;
use Symplify\EasyCodingStandard\SniffRunner\File\File;
use Symplify\EasyCodingStandard\SniffRunner\File\FileFactory;
use Symplify\EasyCodingStandard\SniffRunner\ValueObject\File;
use Symplify\SmartFileSystem\SmartFileInfo;

final class SniffFileProcessor implements FileProcessorInterface, DualRunAwareFileProcessorInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Error\ErrorAndDiffCollector;
use Symplify\EasyCodingStandard\Skipper;
use Symplify\EasyCodingStandard\SniffRunner\ValueObject\File;
use Symplify\SmartFileSystem\SmartFileInfo;

final class FileFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\SniffRunner\File;
namespace Symplify\EasyCodingStandard\SniffRunner\ValueObject;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File as BaseFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PHP_CodeSniffer\Fixer;
use Symplify\EasyCodingStandard\HttpKernel\EasyCodingStandardKernel;
use Symplify\EasyCodingStandard\SniffRunner\File\File;
use Symplify\EasyCodingStandard\SniffRunner\File\FileFactory;
use Symplify\EasyCodingStandard\SniffRunner\ValueObject\File;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Symplify\EasyCodingStandard\SniffRunner\Tests\Error;

use Symplify\EasyCodingStandard\Error\Error;
use Symplify\EasyCodingStandard\Error\ErrorFactory;
use Symplify\EasyCodingStandard\Error\ErrorSorter;
use Symplify\EasyCodingStandard\HttpKernel\EasyCodingStandardKernel;
use Symplify\EasyCodingStandard\ValueObject\Error\Error;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand Down
Loading

0 comments on commit 6fbc65a

Please sign in to comment.