Skip to content

Commit

Permalink
Upgrade package to supports PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
roquie committed Apr 11, 2022
1 parent eb7edeb commit 426d9db
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 90 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and overrides the `defaults` stage.
composer require spacetab-io/configuration
```

#### Versions map

* Package with version `4.*` requires PHP >= `8.1`
* Package with version `3.*` requires PHP >= `7.4` <= `8.0`
* Package with version `2.*` requires PHP < `7.4`

## Usage

By default, path to configuration directory and application stage
Expand Down Expand Up @@ -90,7 +96,7 @@ That all.
## CLI utility

Also, you can install simple small cli-utility to dump total results of merged config.
It possible with multiple ways:
It is possible with multiple ways:

1) Install to `/usr/local/bin` as global binary

Expand Down Expand Up @@ -149,14 +155,14 @@ Help:
## Depends
* \>= PHP 7.4 (for prev version use `2.x` releases of library)
* \>= PHP 8.1
* Composer for install package
## License
The MIT License
Copyright © 2020 spacetab.io, Inc. https://spacetab.io
Copyright © 2022 spacetab.io, Inc. https://spacetab.io
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
}
},
"require": {
"php": ">=7.4",
"php": ">=8.1",
"symfony/yaml": "^4.2 || ^5.0 || ^6.0",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"symfony/console": "^4.3 || ^5.0 || ^6.0",
"spacetab-io/obelix": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"symfony/var-dumper": "^4.2 || ^5.0",
"phpunit/phpunit": "^9 || ^10",
"symfony/var-dumper": "^4.2 || ^5.0 || ^6.0",
"humbug/box": "^3.8",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "^1.5.4",
"spacetab-io/logger": "^2.0"
},
"suggest": {
Expand All @@ -37,5 +37,11 @@
"phpunit --coverage-text",
"phpstan analyse src --level 6"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"ocramius/package-versions": true
}
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
checkMissingIterableValueType: false
80 changes: 34 additions & 46 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Class Configuration
*
* @implements \ArrayAccess<mixed, mixed>
* @implements ArrayAccess<array-key, mixed>
* @package Spacetab\Configuration
*/
final class Configuration implements ConfigurationInterface, ArrayAccess, LoggerAwareInterface
Expand Down Expand Up @@ -59,14 +59,7 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
*/
private Obelix\Dot $config;

/**
* @var string
*/
private string $path;

/**
* @var string
*/
private string $stage;

/**
Expand All @@ -77,8 +70,8 @@ final class Configuration implements ConfigurationInterface, ArrayAccess, Logger
*/
public function __construct(?string $path = null, ?string $stage = null)
{
$envPath = $this->getEnvVariable(self::CONFIG_PATH, self::DEFAULT_CONFIG_PATH);
$envStage = $this->getEnvVariable(self::STAGE, self::DEFAULT_STAGE);
$envPath = self::getEnvVariable(self::CONFIG_PATH, self::DEFAULT_CONFIG_PATH);
$envStage = self::getEnvVariable(self::STAGE, self::DEFAULT_STAGE);

null === $path ? $this->setPath($envPath) : $this->setPath($path);
null === $stage ? $this->setStage($envStage) : $this->setStage($stage);
Expand All @@ -94,7 +87,7 @@ public function __construct(?string $path = null, ?string $stage = null)
* Specially for lazy-based programmers like me.
*
* @param string|null $stage
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*
* @return Configuration
*/
Expand All @@ -104,27 +97,27 @@ public static function auto(?string $stage = null): Configuration
}

/**
* Get's a value from config by dot notation
* E.g get('x.y', 'foo') => returns the value of $config['x']['y']
* Gets a value from config by dot notation
* E.g. get('x.y', 'foo') => returns the value of $config['x']['y']
* And if not exist, return 'foo'.
*
* Supported dot-notation syntax with an asterisk.
* You can read about it here: https://github.com/spacetab-io/obelix-php
*
* @param string $key
* @param mixed $default
* @param mixed|null $default
*
* @return mixed
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
return $this->config->get($key, $default)->getValue();
}

/**
* Gets all the tree config.
*
* @return array<mixed>
* @return array
*/
public function all(): array
{
Expand Down Expand Up @@ -178,7 +171,7 @@ public function getStage(): string
}

/**
* Whether a offset exists
* Whether an offset exists
*
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
Expand All @@ -188,7 +181,7 @@ public function getStage(): string
*
* @return boolean true on success or false on failure.
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return ! empty($this->get($offset));
}
Expand All @@ -204,7 +197,7 @@ public function offsetExists($offset): bool
*
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}
Expand All @@ -220,11 +213,11 @@ public function offsetGet($offset)
* The value to set.
* </p>
* @since 5.0.0
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*
* @return void
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
throw ConfigurationException::operationNotAllowed();
}
Expand All @@ -237,19 +230,19 @@ public function offsetSet($offset, $value)
* The offset to unset.
* </p>
* @since 5.0.0
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*
* @return void
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
throw ConfigurationException::operationNotAllowed();
}

/**
* Initialize all the magic down here
*
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*/
public function load(): Configuration
{
Expand Down Expand Up @@ -288,9 +281,9 @@ public function dump(int $inline = 10, int $indent = 2): string
* Parses configuration and makes a tree of it.
*
* @param string $stage
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*
* @return array<mixed>
* @return array
*/
private function parseConfiguration(string $stage = self::DEFAULT_STAGE): array
{
Expand Down Expand Up @@ -346,38 +339,33 @@ private static function getEnvVariable(string $variable, string $default = ''):
*
* @param array ...$arrays
*
* @return array|mixed
* @return array
*/
private function arrayMergeRecursive(array ...$arrays)
private function arrayMergeRecursive(array ...$arrays): array
{
$base = array_shift($arrays);
if ( ! is_array($base)) {
$base = empty($base) ? [] : [$base];
}

foreach ($arrays as $append) {
if ( ! is_array($append)) {
if (!is_array($append)) {
$append = [$append];
}
foreach ($append as $key => $value) {
if ( ! array_key_exists($key, $base) && ! is_numeric($key)) {
$base[$key] = $append[$key];
if (!array_key_exists($key, $base) && ! is_numeric($key)) {
$base[$key] = $value;
continue;
}

if ((is_array($value) || (isset($base[$key]) && is_array($base[$key]))) && $this->isAssoc($value)) {
$base[$key] = $this->arrayMergeRecursive(
(array) $base[$key],
(array) $append[$key]
(array) $value
);
} else {
if (is_numeric($key)) {
if ( ! in_array($value, $base)) {
$base[] = $value;
}
} else {
$base[$key] = $value;
} else if (is_numeric($key)) {
if (!in_array($value, $base, true)) {
$base[] = $value;
}
} else {
$base[$key] = $value;
}
}
}
Expand All @@ -388,7 +376,7 @@ private function arrayMergeRecursive(array ...$arrays)
/**
* Check if array is associative or sequential list.
*
* @param array<mixed> $array
* @param array $array
*
* @return bool
*/
Expand All @@ -398,19 +386,19 @@ private function isAssoc(array $array): bool
return false;
}

return array_keys($array) !== range(0, count($array) - 1);
return !array_is_list($array);
}

/**
* Automatically find configuration in possible paths.
*
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @throws ConfigurationException
*
* @return string
*/
private static function findDirectories(): string
{
if ($value = trim((string) self::getEnvVariable(self::CONFIG_PATH))) {
if ($value = trim(self::getEnvVariable(self::CONFIG_PATH))) {
// add env config path to top of possible locations.
array_unshift(self::$possibleLocations, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigurationAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ConfigurationAwareInterface
/**
* Sets a configuration instance on the object.
*
* @param \Spacetab\Configuration\ConfigurationInterface $configuration
* @param ConfigurationInterface $configuration
*
* @return void
*/
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigurationAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ trait ConfigurationAwareTrait
/**
* The Configuration instance.
*
* @var \Spacetab\Configuration\ConfigurationInterface
* @var ConfigurationInterface
*/
protected ConfigurationInterface $configuration;

/**
* Sets a configuration.
*
* @param \Spacetab\Configuration\ConfigurationInterface $configuration
* @param ConfigurationInterface $configuration
*/
public function setConfiguration(ConfigurationInterface $configuration): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ interface ConfigurationInterface
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null);
public function get(string $key, mixed $default = null): mixed;

/**
* Gets all the tree config
*
* @return array<mixed>
* @return array
*/
public function all(): array;
}
11 changes: 7 additions & 4 deletions src/Console/DumpConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Spacetab\Configuration\Console;

use Spacetab\Configuration\Configuration;
use Spacetab\Configuration\Exception\ConfigurationException;
use Spacetab\Logger\Logger;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Command\Command;
Expand All @@ -15,6 +16,8 @@

class DumpConfigCommand extends Command
{
protected static $defaultName = 'dump';
protected static $defaultDescription = 'Dump loaded configuration';

protected function configure(): void
{
Expand All @@ -32,9 +35,9 @@ protected function configure(): void
/**
* Execute command, captain.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @throws \Spacetab\Configuration\Exception\ConfigurationException
* @param InputInterface $input
* @param OutputInterface $output
* @throws ConfigurationException
*
* @return int
*/
Expand All @@ -58,6 +61,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$output->writeln($string);

return 0;
return Command::SUCCESS;
}
}
Loading

0 comments on commit 426d9db

Please sign in to comment.