Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 16, 2023
1 parent 60368c8 commit f8f7c66
Show file tree
Hide file tree
Showing 31 changed files with 570 additions and 386 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ jobs:
name: 'PHP Linter'
run: vendor/bin/parallel-lint src tests

-
name: 'Check Commented Code'
run: vendor/bin/easy-ci check-commented-code src tests --ansi

-
name: 'Check Active Classes'
run: vendor/bin/class-leak check src --ansi --skip-type="\Symplify\PhpConfigPrinter\Contract\NodeVisitor\PrePrintNodeVisitorInterface"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ composer.lock
/vendor

.phpunit.cache

# kernel cache
/var
9 changes: 6 additions & 3 deletions bin/config-transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel;
use Symplify\SymplifyKernel\ValueObject\KernelBootAndApplicationRun;

$possibleAutoloadPaths = [
// dependency
Expand All @@ -26,6 +25,10 @@
require_once $scoperAutoloadFilepath;
}

$configTransformerKernel = new ConfigTransformerKernel();
$configTransformerKernel->boot();

$kernelBootAndApplicationRun = new KernelBootAndApplicationRun(ConfigTransformerKernel::class);
$kernelBootAndApplicationRun->run();
$container = $configTransformerKernel->getContainer();

$configTransformerApplication = $container->get(\Symplify\ConfigTransformer\Console\ConfigTransformerApplication::class);
$configTransformerApplication->run();
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"symfony/expression-language": "^6.4",
"symfony/finder": "^6.4",
"symfony/yaml": "^6.4",
"symplify/php-config-printer": "^11.2"
"symplify/php-config-printer": "^11.3",
"webmozart/assert": "^1.11"
},
"require-dev": {
"cweagans/composer-patches": "^1.7",
Expand Down Expand Up @@ -61,7 +62,6 @@
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
"rector": "vendor/bin/rector process --dry-run --ansi",
"release": "vendor/bin/monorepo-builder release patch --ansi"
"rector": "vendor/bin/rector process --dry-run --ansi"
}
}
22 changes: 10 additions & 12 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
use PhpParser\NodeFinder;
use SebastianBergmann\Diff\Differ;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Yaml\Parser;
use Symplify\ConfigTransformer\Console\ColorConsoleDiffFormatter;
use Symplify\ConfigTransformer\Console\ConfigTransformerApplication;
use Symplify\PackageBuilder\Console\Formatter\ColorConsoleDiffFormatter;
use Symplify\PackageBuilder\Console\Output\ConsoleDiffer;
use Symplify\PackageBuilder\Diff\DifferFactory;
use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker;
use Symplify\PackageBuilder\Yaml\ParametersMerger;
use Symplify\ConfigTransformer\Console\ConsoleDiffer;
use Symplify\ConfigTransformer\Console\DifferFactory;
use Symplify\ConfigTransformer\Console\Style\SymfonyStyleFactory;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->defaults()
->public()
->autowire();
->autowire()
->autoconfigure();

$services->load('Symplify\ConfigTransformer\\', __DIR__ . '/../src')
->exclude([
Expand All @@ -35,18 +36,15 @@
$services->alias(Application::class, ConfigTransformerApplication::class);

// color diff
$services->set(DifferFactory::class);
$services->set(Differ::class)
->factory([service(DifferFactory::class), 'create']);

$services->set(ConsoleDiffer::class);

$services->set(ColorConsoleDiffFormatter::class);

$services->set(SymfonyStyle::class)
->factory([service(SymfonyStyleFactory::class), 'create']);

$services->set(BuilderFactory::class);
$services->set(NodeFinder::class);
$services->set(Parser::class);

$services->set(ClassLikeExistenceChecker::class);
$services->set(ParametersMerger::class);
};
31 changes: 0 additions & 31 deletions src/Collector/XmlImportCollector.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/Command/SwitchFormatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\ConfigTransformer\Configuration\ConfigurationFactory;
use Symplify\ConfigTransformer\Converter\ConfigFormatConverter;
use Symplify\ConfigTransformer\FileSystem\ConfigFileDumper;
use Symplify\ConfigTransformer\Finder\ConfigFileFinder;
use Symplify\ConfigTransformer\ValueObject\Configuration;
use Symplify\ConfigTransformer\ValueObject\ConvertedContent;
use Symplify\ConfigTransformer\ValueObject\Option;
use Symplify\SmartFileSystem\SmartFileInfo;

final class SwitchFormatCommand extends Command
{
Expand All @@ -45,7 +45,7 @@ protected function configure(): void
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Path to directory/file with configs',
// 99 % of symfony project has this directory
[getcwd() . '/config']
[getcwd() . '/config', getcwd() . '/app/config']
);

$this->addOption(Option::DRY_RUN, 'n', InputOption::VALUE_NONE, 'Dry run - no removal or config change');
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}

private function removeFileInfo(Configuration $configuration, SmartFileInfo $fileInfo): void
private function removeFileInfo(Configuration $configuration, SplFileInfo $fileInfo): void
{
// only dry run, nothing to remove
if ($configuration->isDryRun()) {
Expand Down
9 changes: 5 additions & 4 deletions src/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\ConfigTransformer\DependencyInjection\ExtensionFaker;
use Symplify\ConfigTransformer\DependencyInjection\Loader\MissingAutodiscoveryDirectoryTolerantYamlFileLoader;
use Symplify\ConfigTransformer\DependencyInjection\Loader\SkippingPhpFileLoader;
Expand All @@ -28,7 +29,7 @@ final class ConfigLoader
* @see https://regex101.com/r/4Uanps/4
* @var string
*/
private const PHP_CONST_REGEX = '#!php/const:?\s*([a-zA-Z0-9_\\\\]+(::[a-zA-Z0-9_]+)?)+(:\s*(.*))?#';
private const PHP_CONST_REGEX = '#!php/const:?\s*([a-zA-Z0-9_\\\]+(::\w+)?)+(:\s*(.*))?#';

/**
* @see https://regex101.com/r/spi4ir/1
Expand All @@ -42,11 +43,11 @@ public function __construct(
}

public function createAndLoadContainerBuilderFromFileInfo(
\SplFileInfo $smartFileInfo,
SplFileInfo $smartFileInfo,
): ContainerBuilderAndFileContent {
$containerBuilder = new ContainerBuilder();

$delegatingLoader = $this->createLoaderBySuffix($containerBuilder, $smartFileInfo->getSuffix());
$delegatingLoader = $this->createLoaderBySuffix($containerBuilder, $smartFileInfo->getExtension());
$fileRealPath = $smartFileInfo->getRealPath();

// correct old syntax of tags so we can parse it
Expand All @@ -59,7 +60,7 @@ public function createAndLoadContainerBuilderFromFileInfo(
static fn (array $match): string => $match[1] . '"' . $match[2] . ($match[4] ?? '') . '"'
);

if (in_array($smartFileInfo->getSuffix(), [Format::YML, Format::YAML], true)) {
if (in_array($smartFileInfo->getExtension(), [Format::YML, Format::YAML], true)) {
$content = Strings::replace(
$content,
self::PHP_CONST_REGEX,
Expand Down
102 changes: 102 additions & 0 deletions src/Console/ColorConsoleDiffFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Console;

use Nette\Utils\Strings;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
* Inspired by @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/Differ/DiffConsoleFormatter.php to be
* used as standalone class, without need to require whole package by Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class ColorConsoleDiffFormatter
{
/**
* @var string
* @see https://regex101.com/r/ovLMDF/1
*/
private const PLUS_START_REGEX = '#^(\+.*)#';

/**
* @var string
* @see https://regex101.com/r/xwywpa/1
*/
private const MINUT_START_REGEX = '#^(\-.*)#';

/**
* @var string
* @see https://regex101.com/r/CMlwa8/1
*/
private const AT_START_REGEX = '#^(@.*)#';

/**
* @var string
* @see https://regex101.com/r/qduj2O/1
*/
private const NEWLINES_REGEX = "#\n\r|\n#";

private readonly string $template;

public function __construct()
{
$this->template = sprintf(
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>' . PHP_EOL,
PHP_EOL,
PHP_EOL
);
}

public function format(string $diff): string
{
return $this->formatWithTemplate($diff, $this->template);
}

private function formatWithTemplate(string $diff, string $template): string
{
$escapedDiff = OutputFormatter::escape(rtrim($diff));

$escapedDiffLines = Strings::split($escapedDiff, self::NEWLINES_REGEX);

// remove description of added + remove; obvious on diffs
foreach ($escapedDiffLines as $key => $escapedDiffLine) {
if ($escapedDiffLine === '--- Original') {
unset($escapedDiffLines[$key]);
}

if ($escapedDiffLine === '+++ New') {
unset($escapedDiffLines[$key]);
}
}

$coloredLines = array_map(function (string $string): string {
$string = $this->makePlusLinesGreen($string);
$string = $this->makeMinusLinesRed($string);
$string = $this->makeAtNoteCyan($string);

if ($string === ' ') {
return '';
}

return $string;
}, $escapedDiffLines);

return sprintf($template, implode(PHP_EOL, $coloredLines));
}

private function makePlusLinesGreen(string $string): string
{
return Strings::replace($string, self::PLUS_START_REGEX, '<fg=green>$1</fg=green>');
}

private function makeMinusLinesRed(string $string): string
{
return Strings::replace($string, self::MINUT_START_REGEX, '<fg=red>$1</fg=red>');
}

private function makeAtNoteCyan(string $string): string
{
return Strings::replace($string, self::AT_START_REGEX, '<fg=cyan>$1</fg=cyan>');
}
}
22 changes: 22 additions & 0 deletions src/Console/ConsoleDiffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Console;

use SebastianBergmann\Diff\Differ;

final class ConsoleDiffer
{
public function __construct(
private readonly Differ $differ,
private readonly ColorConsoleDiffFormatter $colorConsoleDiffFormatter
) {
}

public function diff(string $old, string $new): string
{
$diff = $this->differ->diff($old, $new);
return $this->colorConsoleDiffFormatter->format($diff);
}
}
23 changes: 23 additions & 0 deletions src/Console/DifferFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Console;

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use Symplify\ConfigTransformer\Reflection\PrivatesAccessor;

/**
* @api used in factory
*/
final class DifferFactory
{
public function create(): Differ
{
$unifiedDiffOutputBuilder = new UnifiedDiffOutputBuilder('');
PrivatesAccessor::writePrivateProperty($unifiedDiffOutputBuilder, 'contextLines', 10000);

return new Differ($unifiedDiffOutputBuilder);
}
}
20 changes: 20 additions & 0 deletions src/Console/Style/SymfonyStyleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Symplify\ConfigTransformer\Console\Style;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @api used in factory
*/
final class SymfonyStyleFactory
{
public function create(): SymfonyStyle
{
return new SymfonyStyle(new ArrayInput([]), new ConsoleOutput());
}
}
Loading

0 comments on commit f8f7c66

Please sign in to comment.