Skip to content

Commit

Permalink
Fix error when registering a plugin
Browse files Browse the repository at this point in the history
I'v also added some documentation on how to use it.
Tests are still missing, but I'm not sure how to test it.

Signed-off-by: Jefersson Nathan <malukenho.dev@gmail.com>
  • Loading branch information
malukenho committed Feb 2, 2022
1 parent 4eae228 commit f024e9d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
.phpcs-cache
xulieta.xml
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
🥡 Xulieta Plugin Installer
===========================

This package is part of [codelicia/xulieta][1], and is not intended
to be used separately.

#### How to use it

`Xulieta` will automatically scan dependencies to see if there is
any package that is providing default configurations.

If you want your plugin to take advantage of that functionality,
we expect you to provide some information on your `composer.json`
file, ie:

```json
{
"extra": {
"xulieta": {
"parser": ["Malukenho\\QuoPrimumTempore\\JsonParser"],
"validator": ["Malukenho\\QuoPrimumTempore\\JsonValidator"]
}
}
}
```

---
[1]: https://github.com/codelicia/xulieta


13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
"require": {
"php": ">=8.0",
"ext-dom": "*",
"composer/composer": "^2.1.5",
"symfony/config": "^v5.3.4"
"composer/composer": "^2.2.5",
"symfony/config": "^v5.4.3"
},
"require-dev": {
"doctrine/coding-standard": "^9.0.0",
"malukenho/mcbumpface": "^1.1.5",
"roave/security-advisories": "dev-latest",
"vimeo/psalm": "^4.9.1"
"vimeo/psalm": "^4.19.0"
},
"scripts": {
"post-package-install": "Codelicia\\Xulieta\\AutoPlugin\\Register::scan"
},
"config": {
"allow-plugins": {
"malukenho/mcbumpface": true,
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
6 changes: 6 additions & 0 deletions default-config.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xulieta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/codelicia/xulieta/xulieta.xsd">
<exclude>vendor</exclude>
<exclude>node_modules</exclude>
</xulieta>
85 changes: 44 additions & 41 deletions src/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Composer\Plugin\PluginInterface;
use DOMDocument;
use DOMElement;
use DOMException;
use Symfony\Component\Config\Util\XmlUtils;

use function assert;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static function scan(PackageEvent $event): void
/**
* Retrieve the metadata from the "extra" section
*
* @param array{xulieta: array{parser: string, validator: string}} $extra
* @param array{xulieta?: object|array{parser?: string, validator?: string}} $extra
*
* @return array<string,mixed>
*/
Expand All @@ -91,64 +92,66 @@ private static function getExtraMetadata(array $extra): array
private static function injectModuleIntoConfig(array $extra, IOInterface $io, Composer $composer): void
{
$rootDir = dirname($composer->getConfig()->getConfigSource()->getName());
$xulietaConfigFile = $rootDir . '/xulieta.xml';
$xulietaConfigFile = $readFile = $rootDir . '/xulieta.xml';

// @todo create basic config file in case it doesn't exists?
if (! file_exists($xulietaConfigFile)) {
return;
if (! $io->askConfirmation('Do you want us to create a xulieta.xml for you? ')) {
return;
}

$readFile = __DIR__ . '/../default-config.xml.dist';
}

// FIXME: Filter elements so it doesn't get repeated
$xml = XmlUtils::loadFile($xulietaConfigFile);
$root = $xml->documentElement;
$xml = XmlUtils::loadFile($readFile);

$parsers = $root->getElementsByTagName('parser');
$a = [];
foreach ($parsers->getIterator() as $registeredParsers) {
assert($registeredParsers instanceof DOMElement);
$a[] = $registeredParsers->textContent;
}
self::appendChild($xml, $extra, 'parser');
self::appendChild($xml, $extra, 'validator');

// @todo refactor to functional
foreach ($extra['parser'] as $toBeRegistered) {
if (in_array($toBeRegistered, $a, true)) {
continue;
}
// @fixme: workaround to save properly formatted xml
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->loadXML($xml->saveXML());
$domxml->save($xulietaConfigFile);

$registeredParsers?->parentNode?->insertBefore(
$xml->createElement('parser', $toBeRegistered),
$registeredParsers ?? null
);
}
$io->write('Xulieta configuration is up-to-date...');
}

/** @throws DOMException */
private static function appendChild(DOMDocument $document, array $extra, string $tag): void
{
/** @var DOMElement $root */
$root = $document->documentElement;

$validators = $root->getElementsByTagName('validator');
$validators = $root->getElementsByTagName($tag);
$b = [];

foreach ($validators->getIterator() as $registeredValidators) {
assert($registeredValidators instanceof DOMElement);
$b[] = $registeredValidators->textContent;
foreach ($validators->getIterator() as $taggedElements) {
assert($taggedElements instanceof DOMElement);
$b[] = $taggedElements->textContent;
}

if (! isset($extra[$tag])) {
return;
}

// @todo poorly duplicated code
foreach ($extra['validator'] as $toBeRegistered) {
/** @var string $toBeRegistered */
foreach ($extra[$tag] as $toBeRegistered) {
if (in_array($toBeRegistered, $b, true)) {
continue;
}

$registeredValidators->parentNode->insertBefore(
$xml->createElement('validator', $toBeRegistered),
$registeredValidators ?? null
);
}
if (! isset($taggedElements)) {
$root->append($document->createElement($tag, $toBeRegistered));

// @fixme: workaround to save properly formatted xml
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->loadXML($xml->saveXML());
$domxml->save($xulietaConfigFile);
continue;
}

$io->write('Updating file...');
$taggedElements?->parentNode?->insertBefore(
$document->createElement($tag, $toBeRegistered),
$taggedElements
);
}
}

/** @psalm-return array{post-package-install: string} */
Expand Down

0 comments on commit f024e9d

Please sign in to comment.