Skip to content

Commit

Permalink
Merge 4c275ec into d019d13
Browse files Browse the repository at this point in the history
  • Loading branch information
ABGEO committed May 12, 2020
2 parents d019d13 + 4c275ec commit 43cae75
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
8 changes: 8 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,11 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
$config = (new ConfigResolver())->resolve($cwd);

$app = new App($config);

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

$app->run();
100 changes: 100 additions & 0 deletions src/Command/Initializer/InitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function in_array;
use function glob;
use function basename;
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')
->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;
$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();

return $profileObject->run($input, $output, $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;
}
}
35 changes: 35 additions & 0 deletions src/Command/Initializer/Profiles/InitProfileInterface.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\Initializer\Profiles;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Interface InitProfileInterface
*
* @package Slim\Console\Command\Initializer\Profiles
* @author Temuri Takalandze <me@abgeo.dev>
*/
interface InitProfileInterface
{
/**
* Run new project initialization with profile.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param bool $useDefaultSetup If true, use the default settings for the profile,
* otherwise configure the project interactively.
*
* @return int 0 if everything went fine, or an exit code.
*/
public function run(InputInterface $input, OutputInterface $output, bool $useDefaultSetup = false): int;
}
34 changes: 34 additions & 0 deletions src/Command/Initializer/Profiles/blank/Init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Profiles\blank;

use Slim\Console\Command\Initializer\Profiles\InitProfileInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Init class implementation for profile Blank.
*
* @package Slim\Console\Command\Initializer\Profiles\blank
* @author Temuri Takalandze <me@abgeo.dev>
*/
class Init implements InitProfileInterface
{
/**
* {@inheritDoc}
*/
public function run(InputInterface $input, OutputInterface $output, bool $useDefaultSetup = false): int
{
$output->writeln('Work In Progress!');

return 0;
}
}
7 changes: 7 additions & 0 deletions src/Command/Initializer/Profiles/blank/PROFILE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Profile Blank

Profile doc goes here...

## Authors

- [**Temuri Takalandze**](https://abgeo.dev)

0 comments on commit 43cae75

Please sign in to comment.