Update command factory. Update command collection
This commit is contained in:
parent
0be2f14e95
commit
dbe88b7f2b
7 changed files with 225 additions and 45 deletions
|
@ -2,12 +2,16 @@
|
|||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
class TestCommand {
|
||||
use Cli\Commands\Command;
|
||||
use Runtime\Contracts\Console\Command as CommandInterface;
|
||||
|
||||
class TestCommand extends Command implements CommandInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $signature = 'test';
|
||||
public $signature = 'test:command';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
@ -16,17 +20,26 @@ class TestCommand {
|
|||
|
||||
/**
|
||||
* TestCommand constructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed;
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->ask('What is your name? ');
|
||||
|
||||
$this->printLn($this->argument('test'));
|
||||
|
||||
$this->success('Succcess: Hello ' . $name);
|
||||
$this->warning('Warning: Hello ' . $name);
|
||||
$this->error('Error: Hello ' . $name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
56
src/Cli/Collection/CommandsCollection.php
Normal file
56
src/Cli/Collection/CommandsCollection.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Cli\Collection;
|
||||
|
||||
use Runtime\Contracts\Console\Command;
|
||||
|
||||
class CommandsCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $collection = null;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private static function collect()
|
||||
{
|
||||
$dir = getcwd() . '/app/Console/Commands';
|
||||
if(file_exists($dir)) {
|
||||
foreach (scandir($dir) as $file) {
|
||||
if($file == '.' || $file == '..') continue;
|
||||
|
||||
if(endsWith($file, '.php')) {
|
||||
$class = 'App\Console\Commands\\' . str_replace('.php', '', $file);
|
||||
|
||||
if(!class_exists($class)) continue;
|
||||
|
||||
$executor = app($class);
|
||||
self::$collection[$executor->signature] = $executor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::$collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $name
|
||||
* @return array|Command
|
||||
*/
|
||||
public static function get($name = null)
|
||||
{
|
||||
if(self::$collection == null) {
|
||||
self::collect();
|
||||
}
|
||||
|
||||
if($name != null) {
|
||||
return self::$collection[$name];
|
||||
}
|
||||
|
||||
return self::$collection;
|
||||
}
|
||||
|
||||
}
|
76
src/Cli/Commands/Command.php
Normal file
76
src/Cli/Commands/Command.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Cli\Commands;
|
||||
|
||||
use Cli\Assets\Console;
|
||||
|
||||
class Command
|
||||
{
|
||||
use Console;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $signature = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $args = [];
|
||||
|
||||
/**
|
||||
* Command constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->args = $this->getArgs();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getArgs()
|
||||
{
|
||||
global $argv;
|
||||
|
||||
$args = [];
|
||||
for ($i = 1; $i < count($argv); $i++) {
|
||||
if (preg_match('/^--([^=]+)=(.*)/', $argv[$i], $match)) {
|
||||
$args[$match[1]] = $match[2];
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return string
|
||||
*/
|
||||
public function argument($name)
|
||||
{
|
||||
return $this->args[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Cli\Commands;
|
||||
|
||||
use Cli\Assets\Console;
|
||||
|
||||
class Commands
|
||||
{
|
||||
use Console;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $args = [];
|
||||
|
||||
/**
|
||||
* Commands constructor.
|
||||
* @param array $args
|
||||
*/
|
||||
public function __construct(array $args)
|
||||
{
|
||||
$this->args = $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle console command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->ask('What is your name? ');
|
||||
|
||||
$this->success('Succcess: Hello ' . $name);
|
||||
$this->warning('Warning: Hello ' . $name);
|
||||
$this->error('Error: Hello ' . $name);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,15 +2,31 @@
|
|||
|
||||
namespace Cli;
|
||||
|
||||
use Cli\Commands\Commands;
|
||||
use Cli\Collection\CommandsCollection;
|
||||
|
||||
class Factory
|
||||
{
|
||||
|
||||
public function handle()
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $argv;
|
||||
|
||||
/**
|
||||
* Factory constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
global $argv;
|
||||
app(Commands::class, [$argv])->handle();
|
||||
$this->argv = $argv;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$command = $this->argv[1];
|
||||
|
||||
$executor = CommandsCollection::get($command);
|
||||
$executor->handle();
|
||||
}
|
||||
|
||||
}
|
23
src/Runtime/Contracts/Console/Command.php
Normal file
23
src/Runtime/Contracts/Console/Command.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Runtime\Contracts\Console;
|
||||
|
||||
interface Command
|
||||
{
|
||||
|
||||
/*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription();
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use Runtime\Bootstrap;
|
||||
use Runtime\Contracts\Container\Container;
|
||||
use Runtime\Factory\BootstrapFactory;
|
||||
use Runtime\Http\Input\InputHandler;
|
||||
use Runtime\Http\Request;
|
||||
use Runtime\Http\Response;
|
||||
|
@ -14,7 +15,7 @@ if(!function_exists('app'))
|
|||
/**
|
||||
* @param null $abstract
|
||||
* @param array $arguments
|
||||
* @return \Runtime\Factory\BootstrapFactory|Container
|
||||
* @return BootstrapFactory|Container|mixed
|
||||
*/
|
||||
function app($abstract = null, $arguments = [])
|
||||
{
|
||||
|
@ -54,6 +55,24 @@ if(!function_exists('dump'))
|
|||
}
|
||||
}
|
||||
|
||||
if(!function_exists('endsWith'))
|
||||
{
|
||||
/**
|
||||
* @param $haystack
|
||||
* @param $needle
|
||||
* @return bool
|
||||
*/
|
||||
function endsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
if ($length == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (substr($haystack, -$length) === $needle);
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('input'))
|
||||
{
|
||||
/**
|
||||
|
@ -124,6 +143,20 @@ if(!function_exists('route'))
|
|||
}
|
||||
}
|
||||
|
||||
if(!function_exists('startsWith'))
|
||||
{
|
||||
/**
|
||||
* @param $haystack
|
||||
* @param $needle
|
||||
* @return bool
|
||||
*/
|
||||
function startsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('view'))
|
||||
{
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue