Initial Drupal 11 with DDEV setup
This commit is contained in:
62
vendor/grasmash/expander/.github/workflows/php.yml
vendored
Normal file
62
vendor/grasmash/expander/.github/workflows/php.yml
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
|
||||
build:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: "ubuntu-latest"
|
||||
php: "8.0"
|
||||
coverage: "none"
|
||||
- os: "ubuntu-latest"
|
||||
php: "8.1"
|
||||
coverage: "pcov"
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php }}
|
||||
# Only report coverage once
|
||||
coverage: ${{ matrix.coverage }}
|
||||
|
||||
- name: Validate composer.json and composer.lock
|
||||
run: composer validate --strict
|
||||
|
||||
- name: Cache Composer packages
|
||||
id: composer-cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: vendor
|
||||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-php-
|
||||
|
||||
- name: Install dependencies
|
||||
run: composer -n install --prefer-dist -o
|
||||
|
||||
- name: Run test suite
|
||||
if: matrix.coverage == 'none'
|
||||
run: composer run-script test
|
||||
|
||||
- name: Run coverage
|
||||
if: matrix.coverage == 'pcov'
|
||||
run: composer run-script coverage
|
||||
|
||||
- name: Upload coverage results to Coveralls
|
||||
if: matrix.coverage == 'pcov'
|
||||
env:
|
||||
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: composer run-script coveralls
|
||||
51
vendor/grasmash/expander/.gitignore
vendored
Normal file
51
vendor/grasmash/expander/.gitignore
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
# Cache and logs (Symfony2)
|
||||
/app/cache/*
|
||||
/app/logs/*
|
||||
!app/cache/.gitkeep
|
||||
!app/logs/.gitkeep
|
||||
|
||||
# Email spool folder
|
||||
/app/spool/*
|
||||
|
||||
# Cache, session files and logs (Symfony3)
|
||||
/var/cache/*
|
||||
/var/logs/*
|
||||
/var/sessions/*
|
||||
!var/cache/.gitkeep
|
||||
!var/logs/.gitkeep
|
||||
!var/sessions/.gitkeep
|
||||
|
||||
# Parameters
|
||||
/app/config/parameters.yml
|
||||
/app/config/parameters.ini
|
||||
|
||||
# Managed by Composer
|
||||
/app/bootstrap.php.cache
|
||||
/var/bootstrap.php.cache
|
||||
/bin/*
|
||||
!bin/console
|
||||
!bin/symfony_requirements
|
||||
/vendor/
|
||||
|
||||
# Assets and user uploads
|
||||
/web/bundles/
|
||||
/web/uploads/
|
||||
|
||||
# Assets managed by Bower
|
||||
/web/assets/vendor/
|
||||
|
||||
# PHPUnit
|
||||
/app/phpunit.xml
|
||||
/phpunit.xml
|
||||
|
||||
# Build data
|
||||
/build/
|
||||
|
||||
# Composer PHAR
|
||||
/composer.phar
|
||||
|
||||
# Backup entities generated with doctrine:generate:entities command
|
||||
*/Entity/*~
|
||||
|
||||
.idea
|
||||
.phpunit.result.cache
|
||||
0
vendor/grasmash/expander/CONTRIBUTING.md
vendored
Normal file
0
vendor/grasmash/expander/CONTRIBUTING.md
vendored
Normal file
21
vendor/grasmash/expander/LICENSE.md
vendored
Normal file
21
vendor/grasmash/expander/LICENSE.md
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Matthew Grasmick
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
153
vendor/grasmash/expander/README.md
vendored
Normal file
153
vendor/grasmash/expander/README.md
vendored
Normal file
@ -0,0 +1,153 @@
|
||||
[](https://github.com/grasmash/expander/actions/workflows/php.yml) [](https://packagist.org/packages/grasmash/expander)
|
||||
[](https://packagist.org/packages/grasmash/expander) [](https://coveralls.io/github/grasmash/expander?branch=main)
|
||||
|
||||
This tool expands dot-notated, string property references into their corresponding values. This is useful for run time expansion of property references in configuration files.
|
||||
|
||||
For example implementation, see [Yaml Expander](https://github.com/grasmash/yaml-expander).
|
||||
|
||||
### Installation
|
||||
|
||||
composer require grasmash/expander
|
||||
|
||||
### Example usage:
|
||||
|
||||
Property references use dot notation to indicate array keys, and must be wrapped in `${}`.
|
||||
|
||||
Expansion logic:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
$array = [
|
||||
'type' => 'book',
|
||||
'book' => [
|
||||
'title' => 'Dune',
|
||||
'author' => 'Frank Herbert',
|
||||
'copyright' => '${book.author} 1965',
|
||||
'protaganist' => '${characters.0.name}',
|
||||
'media' => [
|
||||
0 => 'hardcover',
|
||||
1 => 'paperback',
|
||||
],
|
||||
'nested-reference' => '${book.sequel}',
|
||||
],
|
||||
'characters' => [
|
||||
0 => [
|
||||
'name' => 'Paul Atreides',
|
||||
'occupation' => 'Kwisatz Haderach',
|
||||
'aliases' => [
|
||||
0 => 'Usul',
|
||||
1 => 'Muad\'Dib',
|
||||
2 => 'The Preacher',
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'name' => 'Duncan Idaho',
|
||||
'occupation' => 'Swordmaster',
|
||||
],
|
||||
],
|
||||
'summary' => '${book.title} by ${book.author}',
|
||||
'publisher' => '${not.real.property}',
|
||||
'sequels' => '${book.sequel}, and others.',
|
||||
'available-products' => '${book.media.1}, ${book.media.0}',
|
||||
'product-name' => '${${type}.title}',
|
||||
'boolean-value' => true,
|
||||
'expand-boolean' => '${boolean-value}',
|
||||
'null-value' => NULL,
|
||||
'inline-array' => [
|
||||
0 => 'one',
|
||||
1 => 'two',
|
||||
2 => 'three',
|
||||
],
|
||||
'expand-array' => '${inline-array}',
|
||||
'env-test' => '${env.test}',
|
||||
];
|
||||
|
||||
$expander = new Expander();
|
||||
// Optionally set a logger.
|
||||
$expander->setLogger(new Psr\Log\NullLogger());
|
||||
// Optionally set a Stringfier, used to convert array placeholders into strings. Defaults to using implode() with `,` delimeter.
|
||||
// @see StringifierInterface.
|
||||
$expander->setStringifier(new Grasmash\Expander\Stringifier());
|
||||
|
||||
// Parse an array, expanding internal property references.
|
||||
$expanded = $expander->expandArrayProperties($array);
|
||||
|
||||
// Parse an array, expanding references using both internal and supplementary values.
|
||||
$reference_properties = 'book' => ['sequel' => 'Dune Messiah'];
|
||||
// Set an environmental variable.
|
||||
putenv("test=gomjabbar");
|
||||
$expanded = $expander->expandArrayProperties($array, $reference_properties);
|
||||
|
||||
print_r($expanded);
|
||||
````
|
||||
|
||||
Resultant array:
|
||||
|
||||
```php
|
||||
Array
|
||||
(
|
||||
[type] => book
|
||||
[book] => Array
|
||||
(
|
||||
[title] => Dune
|
||||
[author] => Frank Herbert
|
||||
[copyright] => Frank Herbert 1965
|
||||
[protaganist] => Paul Atreides
|
||||
[media] => Array
|
||||
(
|
||||
[0] => hardcover
|
||||
[1] => paperback
|
||||
)
|
||||
|
||||
[nested-reference] => Dune Messiah
|
||||
)
|
||||
|
||||
[characters] => Array
|
||||
(
|
||||
[0] => Array
|
||||
(
|
||||
[name] => Paul Atreides
|
||||
[occupation] => Kwisatz Haderach
|
||||
[aliases] => Array
|
||||
(
|
||||
[0] => Usul
|
||||
[1] => Muad\'Dib
|
||||
[2] => The Preacher
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[1] => Array
|
||||
(
|
||||
[name] => Duncan Idaho
|
||||
[occupation] => Swordmaster
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
[summary] => Dune by Frank Herbert
|
||||
[publisher] => ${not.real.property}
|
||||
[sequels] => Dune Messiah, and others.
|
||||
[available-products] => paperback, hardcover
|
||||
[product-name] => Dune
|
||||
[boolean-value] => true,
|
||||
[expand-boolean] => true,
|
||||
[null-value] =>
|
||||
[inline-array] => Array
|
||||
(
|
||||
[0] => one
|
||||
[1] => two
|
||||
[2] => three
|
||||
)
|
||||
|
||||
[expand-array] => one,two,three
|
||||
[env-test] => gomjabbar
|
||||
[env] => Array
|
||||
(
|
||||
[test] => gomjabbar
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
```
|
||||
11
vendor/grasmash/expander/RELEASE.md
vendored
Normal file
11
vendor/grasmash/expander/RELEASE.md
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Releasing
|
||||
|
||||
### Execute tests
|
||||
|
||||
./scripts/run-tests.sh
|
||||
|
||||
To quickly fix PHPCS issues:
|
||||
|
||||
./scripts/clean-code.sh
|
||||
|
||||
|
||||
60
vendor/grasmash/expander/composer.json
vendored
Normal file
60
vendor/grasmash/expander/composer.json
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "grasmash/expander",
|
||||
"description": "Expands internal property references in PHP arrays file.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"dflydev/dot-access-data": "^3.0.0",
|
||||
"psr/log": "^2 | ^3"
|
||||
},
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Matthew Grasmick"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Grasmash\\Expander\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Grasmash\\Expander\\Tests\\": "tests/src/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"greg-1-anderson/composer-test-scenarios": "^1",
|
||||
"php-coveralls/php-coveralls": "^2.5",
|
||||
"phpunit/phpunit": "^9",
|
||||
"squizlabs/php_codesniffer": "^3.3"
|
||||
},
|
||||
"scripts": {
|
||||
"cs": "phpcs",
|
||||
"cbf": "phpcbf",
|
||||
"unit": "phpunit",
|
||||
"lint": [
|
||||
"find src -name '*.php' -print0 | xargs -0 -n1 php -l",
|
||||
"find tests -name '*.php' -print0 | xargs -0 -n1 php -l"
|
||||
],
|
||||
"test": [
|
||||
"@lint",
|
||||
"@unit",
|
||||
"@cs"
|
||||
],
|
||||
"coverage": "php -d pcov.enabled=1 vendor/bin/phpunit tests/src --coverage-clover build/logs/clover.xml",
|
||||
"coveralls": [
|
||||
"php-coveralls -vvv"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.x-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
3935
vendor/grasmash/expander/composer.lock
generated
vendored
Normal file
3935
vendor/grasmash/expander/composer.lock
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
26
vendor/grasmash/expander/phpcs.xml.dist
vendored
Normal file
26
vendor/grasmash/expander/phpcs.xml.dist
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<ruleset name="Expander">
|
||||
|
||||
<description>Yaml CLI PHP CodeSniffer configuration.</description>
|
||||
|
||||
<arg name="extensions" value="php"/>
|
||||
|
||||
<arg name="colors"/>
|
||||
<arg name="cache" value="build/.phpcs-cache"/>
|
||||
<arg name="parallel" value="10"/>
|
||||
|
||||
<file>.</file>
|
||||
|
||||
<!-- Danger! Exclude patterns apply to the full file path, including parent directories of the current repository. -->
|
||||
<!-- Don't exclude common directory names like `build`, which will fail on Travis CI because of /home/travis/build/acquia/<project>. -->
|
||||
<!-- @see https://github.com/squizlabs/PHP_CodeSniffer/issues/981 -->
|
||||
<exclude-pattern>var/</exclude-pattern>
|
||||
<exclude-pattern>vendor/*</exclude-pattern>
|
||||
<exclude-pattern>tests/resources/*</exclude-pattern>
|
||||
|
||||
<rule ref="PSR2">
|
||||
<exclude name="Generic.Files.LineLength"/>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
18
vendor/grasmash/expander/phpunit.xml.dist
vendored
Normal file
18
vendor/grasmash/expander/phpunit.xml.dist
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- phpunit.xml.dist -->
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||
<coverage processUncoveredFiles="true">
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
<report>
|
||||
<clover outputFile="build/logs/clover.xml"/>
|
||||
</report>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="Yaml Expander Test Suite">
|
||||
<directory>tests/src</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<logging/>
|
||||
</phpunit>
|
||||
310
vendor/grasmash/expander/src/Expander.php
vendored
Normal file
310
vendor/grasmash/expander/src/Expander.php
vendored
Normal file
@ -0,0 +1,310 @@
|
||||
<?php
|
||||
|
||||
namespace Grasmash\Expander;
|
||||
|
||||
use Dflydev\DotAccessData\Data;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* Class Expander
|
||||
* @package Grasmash\Expander
|
||||
*/
|
||||
class Expander implements LoggerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var \Grasmash\Expander\StringifierInterface
|
||||
*/
|
||||
protected StringifierInterface $stringifier;
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setLogger(new NullLogger());
|
||||
$this->setStringifier(new Stringifier());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Grasmash\Expander\StringifierInterface
|
||||
*/
|
||||
public function getStringifier(): StringifierInterface
|
||||
{
|
||||
return $this->stringifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Grasmash\Expander\StringifierInterface $stringifier
|
||||
*/
|
||||
public function setStringifier(StringifierInterface $stringifier)
|
||||
{
|
||||
$this->stringifier = $stringifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Psr\Log\LoggerInterface
|
||||
*/
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
*/
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands property placeholders in an array.
|
||||
*
|
||||
* Placeholders should be formatted as ${parent.child}.
|
||||
*
|
||||
* @param array $array
|
||||
* An array containing properties to expand.
|
||||
*
|
||||
* @return array
|
||||
* The modified array in which placeholders have been replaced with
|
||||
* values.
|
||||
*/
|
||||
public function expandArrayProperties(array $array, $reference_array = []): array
|
||||
{
|
||||
$data = new Data($array);
|
||||
if ($reference_array) {
|
||||
$reference_data = new Data($reference_array);
|
||||
$this->doExpandArrayProperties($data, $array, '', $reference_data);
|
||||
} else {
|
||||
$this->doExpandArrayProperties($data, $array);
|
||||
}
|
||||
|
||||
return $data->export();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the actual property expansion.
|
||||
*
|
||||
* @param Data $data
|
||||
* A data object, containing the $array.
|
||||
* @param array $array
|
||||
* The original, unmodified array.
|
||||
* @param string $parent_keys
|
||||
* The parent keys of the current key in dot notation. This is used to
|
||||
* track the absolute path to the current key in recursive cases.
|
||||
* @param Data|null $reference_data
|
||||
* A reference data object. This is not operated upon but is used as a
|
||||
* reference to provide supplemental values for property expansion.
|
||||
*/
|
||||
protected function doExpandArrayProperties(
|
||||
Data $data,
|
||||
array $array,
|
||||
string $parent_keys = '',
|
||||
?Data $reference_data = null
|
||||
) {
|
||||
foreach ($array as $key => $value) {
|
||||
// Boundary condition(s).
|
||||
if ($value === null || is_bool($value)) {
|
||||
continue;
|
||||
}
|
||||
// Recursive case.
|
||||
if (is_array($value)) {
|
||||
$this->doExpandArrayProperties($data, $value, $parent_keys . "$key.", $reference_data);
|
||||
} else {
|
||||
// Base case.
|
||||
$this->expandStringProperties($data, $parent_keys, $reference_data, $value, $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand a single property.
|
||||
*
|
||||
* @param Data $data
|
||||
* A data object, containing the $array.
|
||||
* @param string $parent_keys
|
||||
* The parent keys of the current key in dot notation. This is used to
|
||||
* track the absolute path to the current key in recursive cases.
|
||||
* @param Data|null $reference_data
|
||||
* A reference data object. This is not operated upon but is used as a
|
||||
* reference to provide supplemental values for property expansion.
|
||||
* @param string $value
|
||||
* The unexpanded property value.
|
||||
* @param string $key
|
||||
* The immediate key of the property.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function expandStringProperties(
|
||||
Data $data,
|
||||
string $parent_keys,
|
||||
?Data $reference_data,
|
||||
string $value,
|
||||
string $key
|
||||
): mixed {
|
||||
$pattern = '/\$\{([^\$}]+)\}/';
|
||||
// We loop through all placeholders in a given string.
|
||||
// E.g., '${placeholder1} ${placeholder2}' requires two replacements.
|
||||
while (str_contains((string) $value, '${')) {
|
||||
$original_value = $value;
|
||||
$value = preg_replace_callback(
|
||||
$pattern,
|
||||
function ($matches) use ($data, $reference_data) {
|
||||
return $this->expandStringPropertiesCallback($matches, $data, $reference_data);
|
||||
},
|
||||
$value,
|
||||
-1,
|
||||
$count
|
||||
);
|
||||
|
||||
// If the value was just a _single_ property reference, we have the opportunity to preserve the data type.
|
||||
if ($count === 1) {
|
||||
preg_match($pattern, $original_value, $matches);
|
||||
if ($matches[0] === $original_value) {
|
||||
$value = $this->expandStringPropertiesCallback($matches, $data, $reference_data);
|
||||
}
|
||||
}
|
||||
|
||||
// If no replacement occurred at all, break to prevent
|
||||
// infinite loop.
|
||||
if ($original_value === $value) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Set value on $data object.
|
||||
if ($parent_keys) {
|
||||
$full_key = $parent_keys . "$key";
|
||||
} else {
|
||||
$full_key = $key;
|
||||
}
|
||||
$data->set($full_key, $value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expansion callback used by preg_replace_callback() in expandProperty().
|
||||
*
|
||||
* @param array $matches
|
||||
* An array of matches created by preg_replace_callback().
|
||||
* @param Data $data
|
||||
* A data object containing the complete array being operated upon.
|
||||
* @param Data|null $reference_data
|
||||
* A reference data object. This is not operated upon but is used as a
|
||||
* reference to provide supplemental values for property expansion.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function expandStringPropertiesCallback(
|
||||
array $matches,
|
||||
Data $data,
|
||||
?Data $reference_data = null
|
||||
): mixed {
|
||||
$property_name = $matches[1];
|
||||
$unexpanded_value = $matches[0];
|
||||
|
||||
// Use only values within the subject array's data.
|
||||
if (!$reference_data) {
|
||||
return $this->expandProperty($property_name, $unexpanded_value, $data);
|
||||
} else {
|
||||
// Search both the subject array's data and the reference data for a value.
|
||||
return $this->expandPropertyWithReferenceData(
|
||||
$property_name,
|
||||
$unexpanded_value,
|
||||
$data,
|
||||
$reference_data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches both the subject data and the reference data for value.
|
||||
*
|
||||
* @param string $property_name
|
||||
* The name of the value for which to search.
|
||||
* @param string $unexpanded_value
|
||||
* The original, unexpanded value, containing the placeholder.
|
||||
* @param Data $data
|
||||
* A data object containing the complete array being operated upon.
|
||||
* @param Data|null $reference_data
|
||||
* A reference data object. This is not operated upon but is used as a
|
||||
* reference to provide supplemental values for property expansion.
|
||||
*
|
||||
* @return string|null The expanded string.
|
||||
* The expanded string.
|
||||
*/
|
||||
public function expandPropertyWithReferenceData(
|
||||
string $property_name,
|
||||
string $unexpanded_value,
|
||||
Data $data,
|
||||
?Data $reference_data
|
||||
): ?string {
|
||||
$expanded_value = $this->expandProperty(
|
||||
$property_name,
|
||||
$unexpanded_value,
|
||||
$data
|
||||
);
|
||||
// If the string was not changed using the subject data, try using
|
||||
// the reference data.
|
||||
if ($expanded_value === $unexpanded_value) {
|
||||
$expanded_value = $this->expandProperty(
|
||||
$property_name,
|
||||
$unexpanded_value,
|
||||
$reference_data
|
||||
);
|
||||
}
|
||||
|
||||
return $expanded_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches a data object for a value.
|
||||
*
|
||||
* @param string $property_name
|
||||
* The name of the value for which to search.
|
||||
* @param string $unexpanded_value
|
||||
* The original, unexpanded value, containing the placeholder.
|
||||
* @param Data $data
|
||||
* A data object containing possible replacement values.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function expandProperty(string $property_name, string $unexpanded_value, Data $data): mixed
|
||||
{
|
||||
if (str_starts_with($property_name, "env.") &&
|
||||
!$data->has($property_name)) {
|
||||
$env_key = substr($property_name, 4);
|
||||
if (isset($_SERVER[$env_key])) {
|
||||
$data->set($property_name, $_SERVER[$env_key]);
|
||||
} elseif (getenv($env_key)) {
|
||||
$data->set($property_name, getenv($env_key));
|
||||
}
|
||||
}
|
||||
|
||||
if (!$data->has($property_name)) {
|
||||
$this->log("Property \${'$property_name'} could not be expanded.");
|
||||
return $unexpanded_value;
|
||||
} else {
|
||||
$expanded_value = $data->get($property_name);
|
||||
if (is_array($expanded_value)) {
|
||||
return $this->getStringifier()->stringifyArray($expanded_value);
|
||||
}
|
||||
$this->log("Expanding property \${'$property_name'} => $expanded_value.");
|
||||
return $expanded_value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message using the logger.
|
||||
*
|
||||
* @param string $message
|
||||
* The message to log.
|
||||
*/
|
||||
public function log(string $message)
|
||||
{
|
||||
$this->getLogger()?->debug($message);
|
||||
}
|
||||
}
|
||||
24
vendor/grasmash/expander/src/Stringifier.php
vendored
Normal file
24
vendor/grasmash/expander/src/Stringifier.php
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Grasmash\Expander;
|
||||
|
||||
/**
|
||||
* Class Stringifier
|
||||
* @package Grasmash\Expander
|
||||
*/
|
||||
class Stringifier implements StringifierInterface
|
||||
{
|
||||
/**
|
||||
* Converts array to string.
|
||||
*
|
||||
* @param array $array
|
||||
* The array to convert.
|
||||
*
|
||||
* @return string
|
||||
* The resultant string.
|
||||
*/
|
||||
public static function stringifyArray(array $array): string
|
||||
{
|
||||
return implode(',', $array);
|
||||
}
|
||||
}
|
||||
17
vendor/grasmash/expander/src/StringifierInterface.php
vendored
Normal file
17
vendor/grasmash/expander/src/StringifierInterface.php
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Grasmash\Expander;
|
||||
|
||||
interface StringifierInterface
|
||||
{
|
||||
/**
|
||||
* Converts array to string.
|
||||
*
|
||||
* @param array $array
|
||||
* The array to convert.
|
||||
*
|
||||
* @return string
|
||||
* The resultant string.
|
||||
*/
|
||||
public static function stringifyArray(array $array): string;
|
||||
}
|
||||
142
vendor/grasmash/expander/tests/src/ExpanderTest.php
vendored
Normal file
142
vendor/grasmash/expander/tests/src/ExpanderTest.php
vendored
Normal file
@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Grasmash\Expander\Tests;
|
||||
|
||||
use Dflydev\DotAccessData\Data;
|
||||
use Grasmash\Expander\Expander;
|
||||
use Grasmash\Expander\Stringifier;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ExpanderTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests Expander::expandArrayProperties().
|
||||
*
|
||||
* @param array $array
|
||||
* @param array $reference_array
|
||||
*
|
||||
* @dataProvider providerSourceData
|
||||
*/
|
||||
public function testExpandArrayProperties(array $array, array $reference_array)
|
||||
{
|
||||
$expander = new Expander();
|
||||
|
||||
$this->setEnvVarFixture('test', 'gomjabbar');
|
||||
|
||||
$expanded = $expander->expandArrayProperties($array);
|
||||
$this->assertEquals('gomjabbar', $expanded['env-test']);
|
||||
$this->assertEquals('Frank Herbert 1965', $expanded['book']['copyright']);
|
||||
$this->assertEquals('Paul Atreides', $expanded['book']['protaganist']);
|
||||
$this->assertEquals('Dune by Frank Herbert', $expanded['summary']);
|
||||
$this->assertEquals('${book.media.1}, hardcover', $expanded['available-products']);
|
||||
$this->assertEquals('Dune', $expanded['product-name']);
|
||||
$this->assertEquals(Stringifier::stringifyArray($array['inline-array']), $expanded['expand-array']);
|
||||
|
||||
$this->assertEquals(true, $expanded['boolean-value']);
|
||||
$this->assertIsBool($expanded['boolean-value']);
|
||||
$this->assertEquals(true, $expanded['expand-boolean']);
|
||||
$this->assertIsBool($expanded['expand-boolean']);
|
||||
|
||||
$expanded = $expander->expandArrayProperties($array, $reference_array);
|
||||
$this->assertEquals('Dune Messiah, and others.', $expanded['sequels']);
|
||||
$this->assertEquals('Dune Messiah', $expanded['book']['nested-reference']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An array of values to test.
|
||||
*/
|
||||
public function providerSourceData(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
'type' => 'book',
|
||||
'book' => [
|
||||
'title' => 'Dune',
|
||||
'author' => 'Frank Herbert',
|
||||
'copyright' => '${book.author} 1965',
|
||||
'protaganist' => '${characters.0.name}',
|
||||
'media' => [
|
||||
0 => 'hardcover',
|
||||
],
|
||||
'nested-reference' => '${book.sequel}',
|
||||
],
|
||||
'characters' => [
|
||||
0 => [
|
||||
'name' => 'Paul Atreides',
|
||||
'occupation' => 'Kwisatz Haderach',
|
||||
'aliases' => [
|
||||
0 => 'Usul',
|
||||
1 => "Muad'Dib",
|
||||
2 => 'The Preacher',
|
||||
],
|
||||
],
|
||||
1 => [
|
||||
'name' => 'Duncan Idaho',
|
||||
'occupation' => 'Swordmaster',
|
||||
],
|
||||
],
|
||||
'summary' => '${book.title} by ${book.author}',
|
||||
'publisher' => '${not.real.property}',
|
||||
'sequels' => '${book.sequel}, and others.',
|
||||
'available-products' => '${book.media.1}, ${book.media.0}',
|
||||
'product-name' => '${${type}.title}',
|
||||
'boolean-value' => true,
|
||||
'expand-boolean' => '${boolean-value}',
|
||||
'null-value' => null,
|
||||
'inline-array' => [
|
||||
0 => 'one',
|
||||
1 => 'two',
|
||||
2 => 'three',
|
||||
],
|
||||
'expand-array' => '${inline-array}',
|
||||
'env-test' => '${env.test}',
|
||||
'test_expanded_to_null' => '${book.expanded_to_null}'
|
||||
],
|
||||
[
|
||||
'book' => [
|
||||
'sequel' => 'Dune Messiah',
|
||||
'expanded_to_null' => null,
|
||||
]
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Expander::expandProperty().
|
||||
*
|
||||
* @dataProvider providerTestExpandProperty
|
||||
*/
|
||||
public function testExpandProperty(array $array, $property_name, $unexpanded_string, $expected)
|
||||
{
|
||||
$data = new Data($array);
|
||||
$expander = new Expander();
|
||||
$expanded_value = $expander->expandProperty($property_name, $unexpanded_string, $data);
|
||||
|
||||
$this->assertEquals($expected, $expanded_value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerTestExpandProperty(): array
|
||||
{
|
||||
return [
|
||||
[ ['author' => 'Frank Herbert'], 'author', '${author}', 'Frank Herbert' ],
|
||||
[ ['book' => ['author' => 'Frank Herbert' ]], 'book.author', '${book.author}', 'Frank Herbert' ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
protected function setEnvVarFixture($key, $value)
|
||||
{
|
||||
putenv("$key=$value");
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user