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

Config Object #14

Merged
merged 44 commits into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
979adff
add ext-json
l0gicgate May 1, 2020
603a6c8
add Config class
l0gicgate May 1, 2020
209c11c
add ConfigResolver
l0gicgate May 1, 2020
21eed65
add periods to error messages
l0gicgate May 1, 2020
2568df6
add getters and setters for Config object
l0gicgate May 1, 2020
6259b75
create AbstractCommand class
l0gicgate May 1, 2020
e907216
add config resolving
l0gicgate May 1, 2020
70b4b3e
rename Application to App
l0gicgate May 2, 2020
a30a337
remove setters and make constructor protected
l0gicgate May 2, 2020
c3e6c17
rename Application to App
l0gicgate May 2, 2020
e2fc479
rename APPLICATION_NAME constant to NAME
l0gicgate May 2, 2020
eaea4e3
fix code style
l0gicgate May 2, 2020
168aa57
remove setConfig
l0gicgate May 2, 2020
e72b1f2
fix typo
l0gicgate May 2, 2020
e29d7b8
move exception to its own aggregate
l0gicgate May 2, 2020
8a89b91
add json_last_error_msg() to exception description
l0gicgate May 2, 2020
c90ab34
rename instances of `Configuration` to `Config`
l0gicgate May 3, 2020
5d02287
change usage of `require_once` to `require`
l0gicgate May 3, 2020
d83aaa4
remove `mergeDefaults` method and refactor `fromEnvironment` static i…
l0gicgate May 3, 2020
542b72c
fix white spacing
l0gicgate May 3, 2020
6cb9b8f
abstract configuration parsers
l0gicgate May 3, 2020
de16735
remove rootDir and refactor validation
l0gicgate May 6, 2020
4035cdc
add micro-optimization within `attemptResolvingConfigFromSupportedFor…
l0gicgate May 6, 2020
442b9b5
add cwd
l0gicgate May 8, 2020
71ec02b
add fromDefaults method
l0gicgate May 8, 2020
4b052a2
add fallback on fromDefaults
l0gicgate May 8, 2020
9132219
refactor parsers
l0gicgate May 8, 2020
6ca9662
add tests
l0gicgate May 8, 2020
8ec4f6c
fix phpstan errors
l0gicgate May 8, 2020
7e10d3c
fix code style errors
l0gicgate May 8, 2020
b8ffb84
fix phpstan errors
l0gicgate May 8, 2020
a17bc29
add is_readable check
l0gicgate May 9, 2020
138d8a0
add more tests
l0gicgate May 9, 2020
590e5f7
remove symfony/config
l0gicgate May 9, 2020
ff8f395
change exception message to be more relevant
l0gicgate May 9, 2020
65b389f
fix docbloc definitions
l0gicgate May 9, 2020
19ad6c5
refactor directory resolving logic
l0gicgate May 9, 2020
aaea1b1
add missing file headers
l0gicgate May 9, 2020
0922b68
fix tests
l0gicgate May 9, 2020
725cecc
fix environment variable resolving logic
l0gicgate May 9, 2020
69deea9
remove unused setter
l0gicgate May 9, 2020
a7072e1
switch resolving logic to is_file instead of file_exists
l0gicgate May 9, 2020
ac0214a
remove checkMissingIterableValueType declaration
l0gicgate May 10, 2020
be36b21
fix type hints
l0gicgate May 10, 2020
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
2 changes: 1 addition & 1 deletion bin/slim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
require __DIR__ . '/../vendor/autoload.php';
}

$config = (new ConfigResolver($cwd))->resolve();
$config = (new ConfigResolver())->resolve($cwd);

$app = new App($config);
$app->run();
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"require": {
"ext-json": "*",
"php": "^7.2",
"symfony/console": "^5.0",
"symfony/config": "^5.0"
"symfony/console": "^5.0"
},
"require-dev": {
"adriansuter/php-autoload-override": "^1.0",
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
parameters:
level: max
checkMissingIterableValueType: false
inferPrivatePropertyTypeFromConstructor: true
4 changes: 3 additions & 1 deletion src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public function getConfig(): ?Config
$app = $this->getApplication();

if ($app instanceof App === false) {
throw new RuntimeException('Method method `getConfig()` does not exist on this type of application.');
throw new RuntimeException(
'Usage of the method `getConfig()` requires the parent application to be a Slim Console Application.'
);
}

return $app->getConfig();
Expand Down
24 changes: 15 additions & 9 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ class Config
public const SLIM_CONSOLE_COMMANDS_DIR = 'SLIM_CONSOLE_COMMANDS_DIR';

/**
* @var array
* @var array<string, string>
*/
protected static $defaults = [
'bootstrapDir' => 'app',
'indexDir' => 'public',
'indexFile' => 'index.php',
'sourceDir' => 'src',
'commandsDir' => 'src/Application/Console/Commands',
'commandsDir' => 'src'
. DIRECTORY_SEPARATOR
. 'Application'
. DIRECTORY_SEPARATOR
. 'Console'
. DIRECTORY_SEPARATOR
. 'Commands',
];

/**
Expand Down Expand Up @@ -118,7 +124,7 @@ public function getCommandsDir(): ?string
}

/**
* @param array $params
* @param array<string, string> $params
*
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -154,7 +160,7 @@ protected static function validate(array $params): void
}

/**
* @param array<mixed> $params
* @param array<string, string> $params
*
* @return Config
*
Expand Down Expand Up @@ -183,11 +189,11 @@ public static function fromArray(array $params): Config
public static function fromEnvironment(): Config
{
return self::fromArray([
'bootstrapDir' => getenv(self::SLIM_CONSOLE_BOOTSTRAP_DIR),
'indexDir' => getenv(self::SLIM_CONSOLE_INDEX_DIR),
'indexFile' => getenv(self::SLIM_CONSOLE_INDEX_FILE),
'sourceDir' => getenv(self::SLIM_CONSOLE_SOURCE_DIR),
'commandsDir' => getenv(self::SLIM_CONSOLE_COMMANDS_DIR),
'bootstrapDir' => (string) getenv(self::SLIM_CONSOLE_BOOTSTRAP_DIR),
'indexDir' => (string) getenv(self::SLIM_CONSOLE_INDEX_DIR),
'indexFile' => (string) getenv(self::SLIM_CONSOLE_INDEX_FILE),
'sourceDir' => (string) getenv(self::SLIM_CONSOLE_SOURCE_DIR),
'commandsDir' => (string) getenv(self::SLIM_CONSOLE_COMMANDS_DIR),
]);
}

Expand Down
52 changes: 25 additions & 27 deletions src/Config/ConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace Slim\Console\Config;

use InvalidArgumentException;
use RuntimeException;
use Slim\Console\Config\Parser\JSONConfigParser;
use Slim\Console\Config\Parser\PHPConfigParser;
use Slim\Console\Exception\CannotParseConfigException;
use Slim\Console\Exception\CannotResolveConfigException;

class ConfigResolver
Expand All @@ -32,34 +32,23 @@ class ConfigResolver
self::FORMAT_JSON,
];

/**
* @var string
*/
protected $cwd;

/**
* @param string $cwd
*/
public function __construct(string $cwd)
{
$this->cwd = $cwd;
}

/**
* Resolve configuration. Environment takes precedence over configuration file.
*
* @param string|null $dir
*
* @return Config
*
* @throws InvalidArgumentException
* @throws CannotParseConfigException
* @throws RuntimeException
*/
public function resolve(): Config
public function resolve(string $dir = null): Config
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
{
try {
return $this->attemptResolvingConfigFromEnvironment();
} catch (CannotResolveConfigException $e) {
try {
return $this->attemptResolvingConfigFromSupportedFormats();
return $this->attemptResolvingConfigFromSupportedFormats($dir);
} catch (CannotResolveConfigException $e) {
return Config::fromDefaults();
}
Expand All @@ -73,12 +62,18 @@ public function resolve(): Config
*/
protected function attemptResolvingConfigFromEnvironment(): Config
{
$bootstrapDir = getenv(Config::SLIM_CONSOLE_BOOTSTRAP_DIR);
$commandsDir = getenv(Config::SLIM_CONSOLE_COMMANDS_DIR);
$indexDir = getenv(Config::SLIM_CONSOLE_INDEX_DIR);
$indexFile = getenv(Config::SLIM_CONSOLE_INDEX_FILE);
$sourceDir = getenv(Config::SLIM_CONSOLE_SOURCE_DIR);

if (
getenv(Config::SLIM_CONSOLE_BOOTSTRAP_DIR)
|| getenv(Config::SLIM_CONSOLE_COMMANDS_DIR)
|| getenv(Config::SLIM_CONSOLE_INDEX_DIR)
|| getenv(Config::SLIM_CONSOLE_INDEX_FILE)
|| getenv(Config::SLIM_CONSOLE_SOURCE_DIR)
(is_string($bootstrapDir) && !empty($bootstrapDir) && !ctype_space($bootstrapDir))
|| (is_string($commandsDir) && !empty($commandsDir) && !ctype_space($commandsDir))
|| (is_string($indexDir) && !empty($indexDir) && !ctype_space($indexDir))
|| (is_string($indexFile) && !empty($indexFile) && !ctype_space($indexFile))
|| (is_string($sourceDir) && !empty($sourceDir) && !ctype_space($sourceDir))
) {
return Config::fromEnvironment();
}
Expand All @@ -87,19 +82,22 @@ protected function attemptResolvingConfigFromEnvironment(): Config
}

/**
* @param string|null $dir
*
* @return Config
*
* @throws CannotResolveConfigException
* @throws InvalidArgumentException
* @throws CannotParseConfigException
* @throws RuntimeException
*/
protected function attemptResolvingConfigFromSupportedFormats(): Config
protected function attemptResolvingConfigFromSupportedFormats(string $dir = null): Config
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
{
$basePath = $this->cwd . DIRECTORY_SEPARATOR . self::CONFIG_FILENAME;
$dir = $dir ?? getcwd();
$basePath = $dir . DIRECTORY_SEPARATOR . self::CONFIG_FILENAME;

foreach ($this->supportedFormats as $format) {
$path = $basePath . ".{$format}";
if (file_exists($path) && is_readable($path)) {
if (is_file($path) && is_readable($path)) {
return $this->attemptParsingConfigFromFile($path, $format);
}
}
Expand All @@ -113,7 +111,7 @@ protected function attemptResolvingConfigFromSupportedFormats(): Config
*
* @return Config
*
* @throws InvalidArgumentException
* @throws CannotParseConfigException
* @throws RuntimeException
*/
protected function attemptParsingConfigFromFile(string $path, string $format): Config
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Parser/ConfigParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Slim\Console\Config\Parser;

use InvalidArgumentException;
use Slim\Console\Config\Config;
use Slim\Console\Exception\CannotParseConfigException;

interface ConfigParserInterface
{
Expand All @@ -20,7 +20,7 @@ interface ConfigParserInterface
*
* @return Config
*
* @throws InvalidArgumentException
* @throws CannotParseConfigException
*/
public static function parse(string $path): Config;
}
4 changes: 1 addition & 3 deletions src/Config/Parser/JSONConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
class JSONConfigParser implements ConfigParserInterface
{
/**
* @param string $path
*
* @return Config
* {@inheritdoc}
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inherit doc?

public static function parse(string $path): Config
{
Expand Down
8 changes: 3 additions & 5 deletions src/Config/Parser/PHPConfigParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@

namespace Slim\Console\Config\Parser;

use InvalidArgumentException;
use Slim\Console\Config\Config;
use Slim\Console\Exception\CannotParseConfigException;

class PHPConfigParser implements ConfigParserInterface
{
/**
* @param string $path
*
* @return Config
* {@inheritdoc}
*/
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
public static function parse(string $path): Config
{
$parsed = require $path;

if (!is_array($parsed)) {
throw new InvalidArgumentException('Slim Console configuration should be an array.');
throw new CannotParseConfigException('Slim Console configuration should be an array.');
}

return Config::fromArray($parsed);
Expand Down
17 changes: 17 additions & 0 deletions src/Exception/CannotParseConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Console/blob/0.x/LICENSE.md (MIT License)
*/

declare(strict_types=1);

namespace Slim\Console\Exception;

use Exception;

class CannotParseConfigException extends Exception
{
}
4 changes: 3 additions & 1 deletion tests/Command/AbstractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function testGetConfigThrowsRuntimeExceptionWithIncompatibleApp(): void
$mockCommand = new MockCommand();

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Method method `getConfig()` does not exist on this type of application.');
$this->expectExceptionMessage(
'Usage of the method `getConfig()` requires the parent application to be a Slim Console Application.'
);

$mockCommand->getConfig();
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Config/ConfigResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function testResolvePrioritizesEnvironment(): void
$this->setupEnvConfig();

$exampleJsonConfigPath = $this->getExampleConfigPath(ConfigResolver::FORMAT_JSON);
$configResolver = new ConfigResolver($exampleJsonConfigPath);
$configResolver = new ConfigResolver();

$config = $configResolver->resolve();
$config = $configResolver->resolve($exampleJsonConfigPath);

$this->assertSame($this->envParams['bootstrapDir'], $config->getBootstrapDir());
$this->assertSame($this->envParams['commandsDir'], $config->getCommandsDir());
Expand All @@ -62,9 +62,9 @@ public function testAttemptResolvingConfigFromSupportedFormats(string $format, C
$example = $parser($configPath);

$exampleConfigPath = $this->getExampleConfigPath($format);
$configResolver = new ConfigResolver($exampleConfigPath);
$configResolver = new ConfigResolver();

$config = $configResolver->resolve();
$config = $configResolver->resolve($exampleConfigPath);

$this->assertSame($example['bootstrapDir'], $config->getBootstrapDir());
$this->assertSame($example['commandsDir'], $config->getCommandsDir());
Expand All @@ -86,7 +86,7 @@ public function testAttemptParsingConfigFromFileThrowsRuntimeException(): void
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Invalid configuration format `{$invalidFormat}`.");

$configResolver = new ConfigResolver($this->examplesConfigBasePath);
$configResolver = new ConfigResolver();

$attemptParsingConfigFromFileMethod->invoke($configResolver, $this->examplesConfigBasePath, $invalidFormat);
}
Expand All @@ -98,8 +98,8 @@ public function testResolveFallbackOnDefaults(): void

$defaults = $defaultsReflection->getValue();

$configResolver = new ConfigResolver($this->examplesConfigBasePath);
$config = $configResolver->resolve();
$configResolver = new ConfigResolver();
$config = $configResolver->resolve($this->examplesConfigBasePath);

$this->assertSame($defaults['bootstrapDir'], $config->getBootstrapDir());
$this->assertSame($defaults['commandsDir'], $config->getCommandsDir());
Expand Down
6 changes: 3 additions & 3 deletions tests/Config/Parser/PHPConfigParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

namespace Slim\Tests\Console\Config\Parser;

use InvalidArgumentException;
use Slim\Console\Config\ConfigResolver;
use Slim\Console\Config\Parser\PHPConfigParser;
use Slim\Console\Exception\CannotParseConfigException;
use Slim\Tests\Console\TestCase;

class PHPConfigParserTest extends TestCase
Expand All @@ -34,13 +34,13 @@ public function testParse(): void
$this->assertSame($phpConfig['sourceDir'], $config->getSourceDir());
}

public function testParseThrowsInvalidArgumentException(): void
public function testParseThrowsExceptionWithInvalidConfigFormat(): void
{
$invalidJsonConfigPath = $this->examplesConfigBasePath
. DIRECTORY_SEPARATOR . 'invalid-php'
. DIRECTORY_SEPARATOR . 'invalid-format.php';

$this->expectException(InvalidArgumentException::class);
$this->expectException(CannotParseConfigException::class);
$this->expectExceptionMessage('Slim Console configuration should be an array.');

PHPConfigParser::parse($invalidJsonConfigPath);
Expand Down
10 changes: 0 additions & 10 deletions tests/Mocks/MockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,4 @@ public function getMockOutput(): string
{
return $this->output;
}

/**
* @param string $output
* @return self
*/
public function setMockOutput(string $output): self
{
$this->output = $output;
return $this;
}
}
8 changes: 8 additions & 0 deletions tests/examples/invalid-php/invalid-format.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<?php

l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Console/blob/0.x/LICENSE.md (MIT License)
*/

declare(strict_types=1);

return "invalid";
Loading