Skip to content

Commit

Permalink
Extract command wrapper to separate package.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Jan 17, 2018
1 parent 209daf0 commit d1d25df
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 1,187 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Create custom commands in minutes to streamline and personalize your workflow.

## Contents
- [Release Notes](#release-notes)
- [Install](#install)
- [Database](#database)
- [Generating Database Directories And Files](#generating-database-directories-and-files)
Expand Down Expand Up @@ -52,6 +53,14 @@
- [Developing](#developing)
- [Credits and Contributing](#credits-and-contributing)

## Release Notes
##### Moving from 1.1.* to 1.2.*
The core command wrapper has been extracted to a separate package
([zachleigh/artisanize](https://github.com/zachleigh/artisanize)). Where possible,
Yarak classes have been maintained for the time being in order to minimize update
issues. However, some interface type declarations may need to be updated after
isntalling the new version.

## Install
### Requirements
This package assumes you have the following:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"vlucas/phpdotenv": "^2.4",
"symfony/filesystem": "^3.2",
"phalcon/zephir": "^0.9.6",
"fzaninotto/faker": "^1.6"
"fzaninotto/faker": "^1.6",
"zachleigh/artisanize": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0",
Expand Down
265 changes: 3 additions & 262 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,268 +2,9 @@

namespace Yarak\Console;

use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Yarak\Console\Input\Input;
use Yarak\Console\Output\SymfonyOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Artisanize\Command as BaseCommand;

abstract class Command extends SymfonyCommand
abstract class Command extends BaseCommand
{
/**
* Symfony console output.
*
* @var SymfonyOutput
*/
protected $output;

/**
* Symfony input implementation.
*
* @var InputInterface
*/
protected $input;

/**
* The command description.
*
* @var string
*/
protected $description;

/**
* Command signature.
*
* @var null|string
*/
protected $signature = null;

/**
* Configure the command if signature is set.
*/
protected function configure()
{
if (!is_null($this->signature)) {
$parser = new SignatureParser($this);

$parser->parse($this->signature);

$this->setDescription($this->description);
}
}

/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = new SymfonyOutput($output);

$this->input = $input;

if (method_exists($this, 'handle')) {
$this->handle();
}
}

/**
* Return the output implementation.
*
* @return SymfonyOutput
*/
protected function getOutput()
{
return $this->output;
}

/**
* Get root symfony output implementation.
*
* @return OutputInterface
*/
protected function getOutputInterface()
{
return $this->getOutput()->getOutput();
}

/**
* Get the value of a command argument.
*
* @param string $key
*
* @return string|array
*/
protected function argument($key = null)
{
if (is_null($key)) {
return $this->input->getArguments();
}

return $this->input->getArgument($key);
}

/**
* Determine if the given argument is present.
*
* @param string|int $name
*
* @return bool
*/
protected function hasArgument($name)
{
return $this->input->hasArgument($name);
}

/**
* Get the value of a command option.
*
* @param string $key
*
* @return string|array
*/
protected function option($key = null)
{
if (is_null($key)) {
return $this->input->getOptions();
}

return $this->input->getOption($key);
}

/**
* Determine if the given option is present.
*
* @param string $name
*
* @return bool
*/
protected function hasOption($name)
{
return $this->input->hasOption($name);
}

/**
* Add input to the command.
*
* @param Input $input
*/
public function addInput(Input $input)
{
$reflection = new \ReflectionClass($input);

$method = 'add'.$reflection->getShortName();

if (method_exists($this, $method)) {
$this->$method(...array_values($input->getAttributes()));
}
}

/**
* Ask for a confirmation.
*
* @param string $text
*
* @return mixed
*/
public function confirm($text)
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion($text, false);

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}

/**
* Ask a question.
*
* @param string $question
* @param mixed|null $default
*
* @return mixed
*/
public function ask($question, $default = null)
{
$helper = $this->getHelper('question');
$question = new Question($question, $default);

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}

/**
* Ask a password.
*
* @param string $question
*
* @return mixed
*/
public function askPassword($question)
{
$helper = $this->getHelper('question');

$question = new Question($question);
$question->setHidden(true);
$question->setHiddenFallback(false);

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}

/**
* Ask a question where the answer is available from a list of predefined choices.
*
* @param string $question
* @param array $choices
* @param mixed|null $default
*
* @return mixed
*/
public function choose($question, array $choices, $default = null)
{
$helper = $this->getHelper('question');
$question = new ChoiceQuestion($question, $choices, $default);
$question->setErrorMessage('%s is not a valid answer.');

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}

/**
* Ask a question where some auto-completion help is provided.
*
* @param string $question
* @param array $autoCompletion
* @param mixed|null $default
*
* @return mixed
*/
public function anticipate($question, array $autoCompletion, $default = null)
{
$helper = $this->getHelper('question');
$question = new Question($question, $default);
$question->setAutocompleterValues($autoCompletion);

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}

/**
* Ask a question where the answer is available from a list of predefined choices and more choices can be selected.
*
* @param string $question
* @param array $choices
* @param mixed|null $default
*
* @return mixed
*/
public function choice($question, array $choices, $default = null)
{
$helper = $this->getHelper('question');
$question = new ChoiceQuestion($question, $choices, $default);
$question->setMultiselect(true);

return $helper->ask($this->input, $this->getOutputInterface(), $question);
}
//
}
88 changes: 3 additions & 85 deletions src/Console/Input/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,9 @@

namespace Yarak\Console\Input;

use Symfony\Component\Console\Input\InputArgument;
use Artisanize\Input\Argument as BaseArgument;

class Argument extends Input
class Argument extends BaseArgument
{
/**
* Default arguement value.
*
* @var null|string
*/
protected $default = null;

/**
* Parse the set argument string.
*
* @return $this
*/
public function parse()
{
return $this->setDescription()
->setArray('IS_ARRAY')
->setOptional()
->setDefault()
->setRequired()
->calculateMode(InputArgument::class)
->setName();
}

/**
* Set optional argument mode.
*
* @return $this
*/
protected function setOptional()
{
if (substr($this->input, -1) === '?') {
$this->modeArray[] = 'OPTIONAL';

$this->input = str_replace('?', '', $this->input);
}

return $this;
}

/**
* Set default option value.
*
* @return $this
*/
protected function setDefault()
{
if (strpos($this->input, '=')) {
list($this->input, $this->default) = explode('=', $this->input);

$this->modeArray[] = 'OPTIONAL';
}

return $this;
}

/**
* Set required mode if optional not already set.
*
* @return $this
*/
protected function setRequired()
{
if (!in_array('OPTIONAL', $this->modeArray)) {
$this->modeArray[] = 'REQUIRED';
}

return $this;
}

/**
* Get the argument attributes.
*
* @return array
*/
public function getAttributes()
{
return [
'name' => $this->name,
'mode' => $this->mode,
'description' => $this->description,
'default' => $this->default,
];
}
//
}
Loading

0 comments on commit d1d25df

Please sign in to comment.