Update command factory. Update command collection

This commit is contained in:
Maarten 2020-01-07 22:15:50 +01:00
parent 0be2f14e95
commit dbe88b7f2b
7 changed files with 225 additions and 45 deletions

View 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;
}
}