Skip to content

Commit

Permalink
Merge b207c47 into 6411187
Browse files Browse the repository at this point in the history
  • Loading branch information
ABGEO committed May 26, 2020
2 parents 6411187 + b207c47 commit bfd3beb
Show file tree
Hide file tree
Showing 16 changed files with 1,196 additions and 48 deletions.
11 changes: 11 additions & 0 deletions bin/slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
declare(strict_types=1);

use Slim\Console\App;
use Slim\Console\Command\Initializer\InitCommand;
use Slim\Console\Config\ConfigResolver;

$cwd = getcwd();
Expand All @@ -23,4 +24,14 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
$config = (new ConfigResolver())->resolve($cwd);

$app = new App($config);

$app->addCommands(
[
new InitCommand(),
]
);

unset($cwd);
unset($config);

$app->run();
107 changes: 59 additions & 48 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
{
"name": "slim/console",
"type": "library",
"description": "Slim Console",
"keywords": ["framework","php","slim"],
"homepage": "https://www.slimframework.com",
"license": "MIT",
"authors": [
{
"name": "Pierre Berube",
"email": "pierre@lgse.com",
"homepage": "http://www.lgse.com"
}
],
"require": {
"ext-json": "*",
"php": "^7.2",
"symfony/console": "^5.0"
},
"require-dev": {
"adriansuter/php-autoload-override": "^1.1",
"phpspec/prophecy": "^1.10",
"phpstan/phpstan": "^0.12.25",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Slim\\Console\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Slim\\Tests\\Console\\": "tests"
}
"name": "slim/console",
"type": "library",
"description": "Slim Console",
"keywords": [
"framework",
"php",
"slim"
],
"homepage": "https://www.slimframework.com",
"license": "MIT",
"authors": [
{
"name": "Pierre Berube",
"email": "pierre@lgse.com",
"homepage": "http://www.lgse.com"
},
"scripts": {
"test": [
"@phpunit",
"@phpcs",
"@phpstan"
],
"phpunit": "phpunit",
"phpcs": "phpcs",
"phpstan": "phpstan analyse src --memory-limit=-1"
},
"config": {
"sort-packages": true
},
"bin": ["bin/slim"]
{
"name": "Temuri Takalandze",
"email": "me@abgeo.dev",
"homepage": "https://abgeo.dev"
}
],
"require": {
"ext-json": "*",
"php": "^7.2",
"symfony/console": "^5.0"
},
"require-dev": {
"adriansuter/php-autoload-override": "^1.1",
"phpspec/prophecy": "^1.10",
"phpstan/phpstan": "^0.12.25",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
"psr-4": {
"Slim\\Console\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Slim\\Tests\\Console\\": "tests"
}
},
"scripts": {
"test": [
"@phpunit",
"@phpcs",
"@phpstan"
],
"phpunit": "phpunit",
"phpcs": "phpcs",
"phpstan": "phpstan analyse src --memory-limit=-1"
},
"config": {
"sort-packages": true
},
"bin": [
"bin/slim"
]
}
108 changes: 108 additions & 0 deletions src/Command/Initializer/InitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Initializer;

use Slim\Console\Command\AbstractCommand;
use Slim\Console\Command\Initializer\Profiles\InitProfileInterface;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function basename;
use function glob;
use function in_array;
use function is_array;
use function is_string;

/**
* Class InitCommand.
*
* @package Slim\Console\Command\Initializer
* @author Temuri Takalandze <me@abgeo.dev>
*/
class InitCommand extends AbstractCommand
{
protected const PROFILE_NAMESPACE_PREFIX = "Slim\Console\Command\Initializer\Profiles";
protected const PROFILE_INIT_CLASS = "Init";

protected static $defaultName = 'init';

/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
->setDescription('Initialize a new Slim project')
->addArgument(
'directory',
InputArgument::REQUIRED,
'Directory of the project to create'
)
->addOption(
'profile',
'p',
InputOption::VALUE_REQUIRED,
'New Slim project skeleton profile',
'blank'
)
->addOption(
'default',
null,
InputOption::VALUE_NONE,
'Use default setup'
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$profileObject = null;
$directory = $input->getArgument('directory');
$directory = is_string($directory) ? $directory : 'new-slim-project';
$profile = $input->getOption('profile');
$profile = is_string($profile) ? $profile : 'blank';
$useDefaultSetup = (bool)$input->getOption('default');

if (!in_array($profile, $this->getAvailableProfiles())) {
throw new InvalidOptionException("Profile `{$profile}` not found!");
}

$profile = self::PROFILE_NAMESPACE_PREFIX . "\\{$profile}\\" . self::PROFILE_INIT_CLASS;
/** @var InitProfileInterface $profileObject */
$profileObject = new $profile($input, $output, $this->getConfig());

return $profileObject->run($directory, $useDefaultSetup);
}

/**
* Get available initialization profiles.
*
* @return array<string>
*/
private function getAvailableProfiles(): array
{
$profiles = [];
$glob = glob(__DIR__ . DIRECTORY_SEPARATOR . 'Profiles' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);

if (is_array($glob)) {
foreach ($glob as $profile) {
$profiles[] = basename($profile);
}
}

return $profiles;
}
}

0 comments on commit bfd3beb

Please sign in to comment.