Skip to content

Commit

Permalink
Merge b8ffb84 into 50e602e
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed May 8, 2020
2 parents 50e602e + b8ffb84 commit 4e05995
Show file tree
Hide file tree
Showing 21 changed files with 944 additions and 6 deletions.
9 changes: 7 additions & 2 deletions bin/slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@

declare(strict_types=1);

use Slim\Console\Application;
use Slim\Console\App;
use Slim\Console\Config\ConfigResolver;

$cwd = getcwd();

if (file_exists(__DIR__ . '/../../../autoload.php')) {
require __DIR__ . '/../../../autoload.php';
} else {
require __DIR__ . '/../vendor/autoload.php';
}

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

$app = new App($config);
$app->run();
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
}
],
"require": {
"ext-json": "*",
"php": "^7.2",
"symfony/console": "^5.0",
"symfony/config": "^5.0"
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
level: max
checkMissingIterableValueType: false
inferPrivatePropertyTypeFromConstructor: true
29 changes: 25 additions & 4 deletions src/Application.php → src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@

namespace Slim\Console;

use Slim\Console\Config\Config;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

class Application extends SymfonyApplication
class App extends SymfonyApplication
{
private const VERSION = '0.1';
protected const NAME = 'Slim Console';

public function __construct()
protected const VERSION = '0.1';

/**
* @var Config
*/
protected $config;

/**
* @param Config $config
*/
public function __construct(Config $config)
{
parent::__construct('Slim Console', self::VERSION);
parent::__construct(static::NAME, static::VERSION);

$this->config = $config;
}

/**
Expand All @@ -48,4 +61,12 @@ public function doRun(InputInterface $input, OutputInterface $output): int

return parent::doRun($input, $output);
}

/**
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}
}
35 changes: 35 additions & 0 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Command;

use RuntimeException;
use Slim\Console\App;
use Slim\Console\Config\Config;
use Symfony\Component\Console\Command\Command;

abstract class AbstractCommand extends Command
{
/**
* @return Config|null
*
* @throws RuntimeException
*/
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.');
}

return $app->getConfig();
}
}
207 changes: 207 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?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\Config;

use InvalidArgumentException;

class Config
{
public const SLIM_CONSOLE_BOOTSTRAP_DIR = 'SLIM_CONSOLE_BOOTSTRAP_DIR';
public const SLIM_CONSOLE_INDEX_DIR = 'SLIM_CONSOLE_INDEX_DIR';
public const SLIM_CONSOLE_INDEX_FILE = 'SLIM_CONSOLE_INDEX_FILE';
public const SLIM_CONSOLE_SOURCE_DIR = 'SLIM_CONSOLE_SOURCE_DIR';
public const SLIM_CONSOLE_COMMANDS_DIR = 'SLIM_CONSOLE_COMMANDS_DIR';

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

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

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

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

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

/**
* @var string|null
*/
protected $commandsDir;

/**
* @param string $bootstrapDir
* @param string $indexDir
* @param string $indexFile
* @param string $sourceDir
* @param string|null $commandsDir
*/
protected function __construct(
string $bootstrapDir,
string $indexDir,
string $indexFile,
string $sourceDir,
?string $commandsDir = null
) {
$this->bootstrapDir = $bootstrapDir;
$this->indexDir = $indexDir;
$this->indexFile = $indexFile;
$this->sourceDir = $sourceDir;
$this->commandsDir = $commandsDir;
}

/**
* @return string
*/
public function getBootstrapDir(): string
{
return $this->bootstrapDir;
}

/**
* @return string
*/
public function getIndexDir(): string
{
return $this->indexDir;
}

/**
* @return string
*/
public function getIndexFile(): string
{
return $this->indexFile;
}

/**
* @return string
*/
public function getSourceDir(): string
{
return $this->sourceDir;
}

/**
* @return string|null
*/
public function getCommandsDir(): ?string
{
return $this->commandsDir;
}

/**
* @param array $params
*
* @throws InvalidArgumentException
*/
protected static function validate(array $params): void
{
[
'bootstrapDir' => $bootstrapDir,
'indexDir' => $indexDir,
'indexFile' => $indexFile,
'sourceDir' => $sourceDir,
'commandsDir' => $commandsDir,
] = $params;

if (!is_string($bootstrapDir) || empty($bootstrapDir) || ctype_space($bootstrapDir)) {
throw new InvalidArgumentException('`bootstrapDir` must be a string.');
}

if (!is_string($indexDir) || empty($indexDir) || ctype_space($indexDir)) {
throw new InvalidArgumentException('`indexDir` must be a string.');
}

if (!is_string($indexFile) || empty($indexFile) || ctype_space($indexFile)) {
throw new InvalidArgumentException('`indexFile` must be a string.');
}

if (!is_string($sourceDir) || empty($sourceDir) || ctype_space($sourceDir)) {
throw new InvalidArgumentException('`sourceDir` must be a string.');
}

if (!empty($commandsDir) && (!is_string($commandsDir) || ctype_space($commandsDir))) {
throw new InvalidArgumentException('`commandsDir` must be a string.');
}
}

/**
* @param array<mixed> $params
*
* @return Config
*
* @throws InvalidArgumentException
*/
public static function fromArray(array $params): Config
{
$params = array_merge(self::$defaults, $params);

self::validate($params);

return new self(
$params['bootstrapDir'],
$params['indexDir'],
$params['indexFile'],
$params['sourceDir'],
$params['commandsDir']
);
}

/**
* @return Config
*
* @throws InvalidArgumentException
*/
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),
]);
}

/**
* @return Config
*/
public static function fromDefaults(): Config
{
return new self(
self::$defaults['bootstrapDir'],
self::$defaults['indexDir'],
self::$defaults['indexFile'],
self::$defaults['sourceDir'],
self::$defaults['commandsDir']
);
}
}

0 comments on commit 4e05995

Please sign in to comment.