Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add MigrateEmailFlagToEmailTypeFlexFormFractor #72

Merged
merged 3 commits into from
Jun 9, 2024
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ return FractorConfiguration::configure()
]);
```

### Configure code style

Fractor tries to format the code as good as possible.
If you want to adjust the indentation of your xml files, you can configure it this way:

```php
<?php

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\Fractor\ValueObject\Indent;
use a9f\FractorXml\Configuration\XmlProcessorOption;

return FractorConfiguration::configure()
->withOptions([
XmlProcessorOption::INDENT_CHARACTER => Indent::STYLE_TAB,
XmlProcessorOption::INDENT_SIZE => 1,
])
```

## Processing

Execute Fractor from the command line, passing the path to your configuration file as an argument:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"nette/utils": "^4.0",
"phpstan/phpstan": "^1.10.9",
"sebastian/diff": "^5.0",
"shanethehat/pretty-xml": "^1.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
Expand Down
1 change: 1 addition & 0 deletions packages/fractor-xml/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ext-xml": "*",
"a9f/fractor": "^0.2",
"a9f/fractor-extension-installer": "^0.2",
"shanethehat/pretty-xml": "^1.0",
"webmozart/assert": "^1.11"
},
"require-dev": {
Expand Down
12 changes: 12 additions & 0 deletions packages/fractor-xml/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

declare(strict_types=1);

use a9f\Fractor\ValueObject\Indent;
use a9f\FractorXml\Contract\Formatter;
use a9f\FractorXml\Contract\XmlFractor;
use a9f\FractorXml\IndentFactory;
use a9f\FractorXml\PrettyXmlFormatter;
use a9f\FractorXml\XmlFileProcessor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
Expand All @@ -16,8 +21,15 @@

$services->load('a9f\\FractorXml\\', __DIR__ . '/../src/');

$services->set('fractor.xml_processor.indent', Indent::class)
->factory([service(IndentFactory::class), 'create']);

$services->set(XmlFileProcessor::class)
->arg('$indent', service('fractor.xml_processor.indent'))
->arg('$rules', tagged_iterator('fractor.xml_rule'));

$services->set(\PrettyXml\Formatter::class);
$services->alias(Formatter::class, PrettyXmlFormatter::class);

$containerBuilder->registerForAutoconfiguration(XmlFractor::class)->addTag('fractor.xml_rule');
};
6 changes: 3 additions & 3 deletions packages/fractor-xml/src/AbstractXmlFractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

abstract class AbstractXmlFractor implements DomNodeVisitor, XmlFractor
{
private ?File $file = null;
protected ?File $file = null;

public function beforeTraversal(File $file, \DOMNode $rootNode): void
public function beforeTraversal(File $file, \DOMDocument $rootNode): void
{
$this->file = $file;
}
Expand All @@ -41,7 +41,7 @@ public function leaveNode(\DOMNode $node): void
// no-op for now
}

public function afterTraversal(\DOMNode $rootNode): void
public function afterTraversal(\DOMDocument $rootNode): void
{
// no-op for now
}
Expand Down
12 changes: 12 additions & 0 deletions packages/fractor-xml/src/Configuration/XmlProcessorOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace a9f\FractorXml\Configuration;

final class XmlProcessorOption
{
public const INDENT_SIZE = 'xml-processor-indent-size';

public const INDENT_CHARACTER = 'xml-processor-indent-character';
}
4 changes: 2 additions & 2 deletions packages/fractor-xml/src/Contract/DomNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
interface DomNodeVisitor
{
public function beforeTraversal(File $file, \DOMNode $rootNode): void;
public function beforeTraversal(File $file, \DOMDocument $rootNode): void;

/**
* @return \DOMNode|DomDocumentIterator::*
Expand All @@ -21,5 +21,5 @@ public function enterNode(\DOMNode $node): \DOMNode|int;

public function leaveNode(\DOMNode $node): void;

public function afterTraversal(\DOMNode $rootNode): void;
public function afterTraversal(\DOMDocument $rootNode): void;
}
12 changes: 12 additions & 0 deletions packages/fractor-xml/src/Contract/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace a9f\FractorXml\Contract;

use a9f\Fractor\ValueObject\Indent;

interface Formatter
{
public function format(Indent $indent, string $content): string;
}
29 changes: 29 additions & 0 deletions packages/fractor-xml/src/IndentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace a9f\FractorXml;

use a9f\Fractor\ValueObject\Indent;
use a9f\FractorXml\Configuration\XmlProcessorOption;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

final readonly class IndentFactory
{
public function __construct(
private ContainerBagInterface $parameterBag
) {
}

public function create(): Indent
{
$size = $this->parameterBag->has(XmlProcessorOption::INDENT_SIZE) ? $this->parameterBag->get(
XmlProcessorOption::INDENT_SIZE
) : 4;
$style = $this->parameterBag->has(XmlProcessorOption::INDENT_CHARACTER) ? $this->parameterBag->get(
XmlProcessorOption::INDENT_CHARACTER
) : Indent::STYLE_SPACE;

return Indent::fromSizeAndStyle($size, $style);
}
}
25 changes: 25 additions & 0 deletions packages/fractor-xml/src/PrettyXmlFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace a9f\FractorXml;

use a9f\Fractor\ValueObject\Indent;
use a9f\FractorXml\Contract\Formatter;

final readonly class PrettyXmlFormatter implements Formatter
{
public function __construct(
private \PrettyXml\Formatter $prettyXmlFormatter
) {
}

public function format(Indent $indent, string $content): string
{
$indentCharacter = $indent->isSpace() ? Indent::CHARACTERS[Indent::STYLE_SPACE] : Indent::CHARACTERS[Indent::STYLE_TAB];
$this->prettyXmlFormatter->setIndentCharacter($indentCharacter);
$this->prettyXmlFormatter->setIndentSize($indent->length());

return $this->prettyXmlFormatter->format($content);
}
}
7 changes: 6 additions & 1 deletion packages/fractor-xml/src/XmlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use a9f\Fractor\Application\Contract\FileProcessor;
use a9f\Fractor\Application\ValueObject\File;
use a9f\Fractor\Exception\ShouldNotHappenException;
use a9f\Fractor\ValueObject\Indent;
use a9f\FractorXml\Contract\Formatter;
use a9f\FractorXml\Contract\XmlFractor;
use a9f\FractorXml\ValueObjectFactory\DomDocumentFactory;

Expand All @@ -20,7 +22,9 @@
*/
public function __construct(
private DomDocumentFactory $domDocumentFactory,
private iterable $rules
private Formatter $formatter,
private iterable $rules,
private Indent $indent
) {
}

Expand All @@ -42,6 +46,7 @@ public function handle(File $file, iterable $appliedRules): void
$iterator->traverseDocument($file, $document);

$newXml = $this->saveXml($document);
$newXml = $this->formatter->format($this->indent, $newXml);

if ($newXml === $originalXml) {
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/fractor-xml/tests/DomDocumentIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function __construct(
) {
}

public function beforeTraversal(File $file, \DOMNode $rootNode): void
public function beforeTraversal(File $file, \DOMDocument $rootNode): void
{
$this->calls[] = sprintf('%s:beforeTraversal:%s', $this->visitorName, $rootNode->nodeName);
}
Expand All @@ -247,7 +247,7 @@ public function leaveNode(\DOMNode $node): void
$this->calls[] = sprintf('%s:leaveNode:%s', $this->visitorName, $node->nodeName);
}

public function afterTraversal(\DOMNode $rootNode): void
public function afterTraversal(\DOMDocument $rootNode): void
{
$this->calls[] = sprintf('%s:afterTraversal:%s', $this->visitorName, $rootNode->nodeName);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/fractor/src/Configuration/FractorConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ final class FractorConfigurationBuilder
*/
private array $imports = [];

/**
* @var array<string, int|string>
*/
private array $options = [];

public function __invoke(ContainerConfigurator $containerConfigurator): void
{
Assert::allString($this->paths);
Expand All @@ -49,6 +54,10 @@ public function __invoke(ContainerConfigurator $containerConfigurator): void
$parameters->set(Option::PATHS, $this->paths);
$parameters->set(Option::SKIP, $this->skip);

foreach ($this->options as $optionName => $optionValue) {
$parameters->set($optionName, $optionValue);
}

$services = $containerConfigurator->services();

foreach ($this->rules as $rule) {
Expand Down Expand Up @@ -146,4 +155,14 @@ public function import(string $import): self

return $this;
}

/**
* @param array<string, string|int> $options
*/
public function withOptions(array $options): self
{
$this->options = $options;

return $this;
}
}
35 changes: 35 additions & 0 deletions packages/fractor/src/Helper/ArrayUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace a9f\Fractor\Helper;

class ArrayUtility
{
/**
* @return string[]
*
* @see GeneralUtility::trimExplode in TYPO3 Core
*/
public static function trimExplode(string $delimiter, string $string, bool $removeEmptyValues = false): array
{
if ($delimiter === '') {
throw new \InvalidArgumentException('Please define a correct delimiter');
}

$result = explode($delimiter, $string);

if ($removeEmptyValues) {
$temp = [];
foreach ($result as $value) {
if (trim($value) !== '') {
$temp[] = $value;
}
}

$result = $temp;
}

return array_map('trim', $result);
}
}
22 changes: 22 additions & 0 deletions packages/fractor/src/Helper/StringUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace a9f\Fractor\Helper;

class StringUtility
{
/**
* Check if an item exists in a comma-separated list of items.
*
* @param string $list Comma-separated list of items (string)
* @param string $item Item to check for
* @return bool TRUE if $item is in $list
*
* @see GeneralUtility::inList in TYPO3 Core
*/
public static function inList(string $list, string $item): bool
{
return str_contains(',' . $list . ',', ',' . $item . ',');
}
}
15 changes: 11 additions & 4 deletions packages/fractor/src/ValueObject/Indent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
*/
final readonly class Indent
{
public const STYLE_SPACE = 'space';

public const STYLE_TAB = 'tab';

/**
* @var array<string, string>
*/
public const CHARACTERS = [
'space' => ' ',
'tab' => "\t",
self::STYLE_SPACE => ' ',
self::STYLE_TAB => "\t",
];

private function __construct(
Expand All @@ -41,15 +45,18 @@ public function toString(): string

public function isSpace(): bool
{
return \preg_match('#^( +).*#', $this->value) === 1;
return \preg_match('/^( +)/', $this->value) === 1;
}

public function length(): int
{
return strlen($this->value);
}

private static function fromSizeAndStyle(int $size, string $style): self
/**
* @phpstan-param self::STYLE_* $style
*/
public static function fromSizeAndStyle(int $size, string $style): self
{
Assert::greaterThanEq($size, 1);
Assert::keyExists(self::CHARACTERS, $style);
Expand Down
Loading