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

Commit

Permalink
fix loading with non root config
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 10, 2020
1 parent e4798be commit 2747fd6
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 56 deletions.
20 changes: 14 additions & 6 deletions .github/workflows/ecs_prefixed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ jobs:
-
name: Run ecs.phar
run: |
cd packages/EasyCodingStandard/compiler
../tmp/ecs.phar
# remove local vendor, to prevent duplicated content
rm -rf packages/EasyCodingStandard/vendor
cd packages/EasyCodingStandard
tmp/ecs.phar
-
name: Run ecs.phar with PSR-12 set
run: |
cd packages/EasyCodingStandard/compiler
../tmp/ecs.phar check ../src --set psr12 --dry-run --debug
cd packages/EasyCodingStandard
# create dummy file
echo "<?php echo 'hi';" >> someFile.php
tmp/ecs.phar check someFile.php --set dead-code
# Deploy PHAR to https://github.com/Symplify/EasyCodingStandardPrefixed
-
Expand All @@ -57,19 +61,23 @@ jobs:
# reuse tmp/ecs.phar from previous job
git clone https://${ACCESS_TOKEN}@github.com/Symplify/EasyCodingStandardPrefixed.git > /dev/null 2>&1
# copy phar files inside cloned repository
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs.phar
cp tmp/ecs.phar EasyCodingStandardPrefixed/ecs
cd EasyCodingStandardPrefixed
# go to clone repository
cd EasyCodingStandardPrefixed
git config user.email "[email protected]"
git config user.name "Github Actions"
git add ecs ecs.phar
# commit with new tag, if this commit is tagged, or with normal commit
if [ "${TRAVIS_TAG}" != "" ]; then COMMIT_MSG="ECS ${TRAVIS_TAG}"; else COMMIT_MSG="Updated ECS to commit ${TRAVIS_COMMIT}"; fi
git commit -m "${COMMIT_MSG}"
git push --quiet origin master
# push tag, if this commit is tagged, or with normal push
if [ "${TRAVIS_TAG}" != "" ]; then git tag "${TRAVIS_TAG}" && git push --quiet origin ${TRAVIS_TAG}; fi
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
9 changes: 3 additions & 6 deletions packages/EasyCodingStandard/bin/ecs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ $autoloadIncluder->autoloadProjectAutoloaderFile('/../../autoload.php');
$autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
$autoloadIncluder->includePhpCodeSnifferAutoloadIfNotInPharAndInitliazeTokens();



# 2. create container
$configs = [];

Expand All @@ -35,7 +33,6 @@ $input = new ArgvInput();
$inputConfig = $configResolver->resolveFromInputWithFallback($input,
['easy-coding-standard.yml', 'easy-coding-standard.yaml', 'ecs.yml', 'ecs.yaml']
);

if ($inputConfig) {
$configs[] = $inputConfig;
}
Expand All @@ -48,7 +45,6 @@ if ($setInputConfig) {

// 3. "parameters > set" in provided yaml files
$parameterSetsConfigs = $configResolver->resolveFromParameterSetsFromConfigFiles($configs, Set::SET_DIRECTORY);

if ($parameterSetsConfigs !== []) {
$configs = array_merge($configs, $parameterSetsConfigs);
}
Expand Down Expand Up @@ -76,8 +72,9 @@ foreach ($configs as $config) {
}
}

$mainConfigShiftedAsLast[] = $inputConfig;

if ($inputConfig !== null) {
$mainConfigShiftedAsLast[] = $inputConfig;
}

$easyCodingStandardKernel = new EasyCodingStandardKernel($environment, InputDetector::isDebug());
if ($configs !== []) {
Expand Down
13 changes: 13 additions & 0 deletions packages/EasyCodingStandard/compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ php ../tmp/ecs.phar
```

Please note that running the compiler will change the contents of `composer.json` file and `vendor` directory. Revert those changes after running it.

## Notes

This section si needed in `composer.json`, because it was causing autolaoding bugs.
Box aliases existing Symfony stubs to php, see https://ayesh.me/composer-replace-polyfills.

```json
{
"replace": {
"symfony/polyfill-php70": "*"
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Nette\Utils\FileSystem as NetteFileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use PharIo\Version\Version;
use Symfony\Component\Console\Command\Command;
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\Packagist\SymplifyStableVersionProvider;
use Symplify\EasyCodingStandard\Compiler\Process\CompileProcessFactory;

/**
Expand All @@ -35,11 +34,6 @@ final class CompileCommand extends Command
*/
private $originalComposerJsonFileContent;

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

/**
* @var Filesystem
*/
Expand All @@ -50,13 +44,19 @@ final class CompileCommand extends Command
*/
private $compileProcessFactory;

/**
* @var SymplifyStableVersionProvider
*/
private $symplifyStableVersionProvider;

public function __construct(CompileProcessFactory $compileProcessFactory, string $dataDir, string $buildDir)
{
parent::__construct();
$this->filesystem = new Filesystem();
$this->compileProcessFactory = $compileProcessFactory;
$this->dataDir = $dataDir;
$this->buildDir = $buildDir;
$this->symplifyStableVersionProvider = new SymplifyStableVersionProvider();
}

protected function configure(): void
Expand All @@ -78,12 +78,6 @@ 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 Down Expand Up @@ -120,32 +114,9 @@ private function restoreComposerJson(string $composerJsonFile): void
// re-run @todo composer update on root
}

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

$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 = key($symplifyPackageVersions);
$lastStableVersion = new Version($lastStableVersion);

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

return $this->symplifyVersionToRequire;
}

private function replaceDevSymplifyVersionWithLastStableVersion(array $json): array
{
$symplifyVersionToRequire = $this->getSymplifyStableVersionToRequire();
$symplifyVersionToRequire = $this->symplifyStableVersionProvider->provide();

foreach (array_keys($json['require']) as $package) {
/** @var string $package */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Compiler\Packagist;

use Nette\Utils\Json;
use PharIo\Version\Version;
use Symplify\EasyCodingStandard\Compiler\Exception\ShouldNotHappenException;

final class SymplifyStableVersionProvider
{
/**
* @var string|null
*/
private $symplifyVersionToRequire;

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

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

$symplifyPackageJson = $this->loadContentJsonStringToArray($symplifyPackageContent);
$symplifyPackageVersions = $symplifyPackageJson['packages']['symplify/symplify'];

$lastStableVersion = $this->getLastKey($symplifyPackageVersions);

$lastStableVersion = new Version($lastStableVersion);

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

return $this->symplifyVersionToRequire;
}

private function loadContentJsonStringToArray(string $jsonContent): array
{
return Json::decode($jsonContent, Json::FORCE_ARRAY);
}

private function getLastKey(array $items): string
{
end($items);

return key($items);
}
}
24 changes: 23 additions & 1 deletion packages/EasyCodingStandard/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"symplify/autowire-array-parameter": "^7.3",
"symplify/auto-bind-parameter": "^7.3"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"symplify/easy-coding-standard-tester": "^7.3"
},
"autoload": {
"psr-4": {
"Symplify\\EasyCodingStandard\\": "src",
Expand All @@ -38,5 +42,23 @@
"Symplify\\EasyCodingStandard\\FixerRunner\\": "packages/FixerRunner/src",
"Symplify\\EasyCodingStandard\\SniffRunner\\": "packages/SniffRunner/src"
}
}
},
"autoload-dev": {
"psr-4": {
"Symplify\\EasyCodingStandard\\ChangedFilesDetector\\Tests\\": "packages/ChangedFilesDetector/tests",
"Symplify\\EasyCodingStandard\\FixerRunner\\Tests\\": "packages/FixerRunner/tests",
"Symplify\\EasyCodingStandard\\SniffRunner\\Tests\\": "packages/SniffRunner/tests",
"Symplify\\EasyCodingStandard\\Tests\\": "tests"
}
},
"replace": {
"symfony/polyfill-php70": "*"
},
"extra": {
"branch-alias": {
"dev-master": "7.3-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# updated to PHP CS Fixer v2.11.0
# updated to PHP CS Fixer v2.11.0 from https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/RuleSet.php

services:
# PSR1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function createFromContainerBuilderAndKernel(
ContainerBuilder $containerBuilder,
KernelInterface $kernel
): DelegatingLoader {
return $this->createFromContainerBuilderAndFileLocator($containerBuilder, new FileLocator($kernel));
$kernelFileLocator = new FileLocator($kernel);

return $this->createFromContainerBuilderAndFileLocator($containerBuilder, $kernelFileLocator);
}

/**
Expand All @@ -29,7 +31,8 @@ public function createContainerBuilderAndConfig(
ContainerBuilder $containerBuilder,
string $config
): DelegatingLoader {
$fileLocator = new SimpleFileLocator(dirname($config));
$directory = dirname($config);
$fileLocator = new SimpleFileLocator($directory);

return $this->createFromContainerBuilderAndFileLocator($containerBuilder, $fileLocator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ protected function build(ContainerBuilder $containerBuilder): void
*/
protected function getContainerLoader(ContainerInterface $container): DelegatingLoader
{
return (new DelegatingLoaderFactory())->createFromContainerBuilderAndKernel($container, $this);
$delegatingLoaderFactory = new DelegatingLoaderFactory();

return $delegatingLoaderFactory->createFromContainerBuilderAndKernel($container, $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public function __construct(ContainerBuilder $containerBuilder, FileLocatorInter
*/
protected function loadFile($file)
{
var_dump($file);

/** @var mixed[]|null $configuration */
$configuration = parent::loadFile($file);
if ($configuration === null) {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ parameters:

- '#Strict comparison using \=\=\= between string\|false and null will always evaluate to false#'
- '#Parameter \#1 \$json of static method Nette\\Utils\\Json\:\:decode\(\) expects string, string\|false given#'
- '#Parameter \#1 \$jsonContent of method Symplify\\EasyCodingStandard\\Compiler\\Packagist\\SymplifyStableVersionProvider\:\:loadContentJsonStringToArray\(\) expects string, string\|false given#'
- '#Method Symplify\\EasyCodingStandard\\Compiler\\Packagist\\SymplifyStableVersionProvider\:\:getLastKey\(\) should return string but returns int\|string\|null#'

0 comments on commit 2747fd6

Please sign in to comment.