simple-php/src/Cli/Collection/CommandsCollection.php

56 lines
No EOL
1.1 KiB
PHP

<?php
namespace Cli\Collection;
use Cli\Commands\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()->make($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;
}
}