Update command factory. Update command collection
This commit is contained in:
parent
0be2f14e95
commit
dbe88b7f2b
7 changed files with 225 additions and 45 deletions
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue