Use ICollections instead of arrays

2.x 1.1.1
Julien Rosset 2 years ago
parent d4353cb500
commit 1f5216a340

@ -6,8 +6,9 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^7.4 || ^8.0", "php": "^7.4 || ^8.0",
"symfony/console": "^5.4 || ^6.0",
"jrosset/betterphptoken": "^1.0", "jrosset/betterphptoken": "^1.0",
"symfony/console": "^5.4 || ^6.0" "jrosset/collections": "^2.3"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -2,48 +2,28 @@
namespace jrosset\CliProgram; namespace jrosset\CliProgram;
use jrosset\Collections\Collection;
use jrosset\Collections\ICollection;
/** /**
* An application with command auto discovery * An application with command auto discovery
*/ */
trait TAutoDiscoveryApplication { trait TAutoDiscoveryApplication {
/** /**
* @var IAutoDiscoverySpot[] The list of discovery spots * @var ICollection<IAutoDiscoverySpot> The list of discovery spots
*/ */
private array $autoDiscoverySpots = []; private ICollection $autoDiscoverySpots;
/** /**
* The list of discovery spots * The list of discovery spots
* *
* @return IAutoDiscoverySpot[] The list of discovery spots * @return ICollection<IAutoDiscoverySpot> The list of discovery spots
*/ */
public function getAutoDiscoverySpots (): array { public function getAutoDiscoverySpots (): ICollection {
return $this->autoDiscoverySpots; if (!isset($this->autoDiscoverySpots)) {
} $this->autoDiscoverySpots = new Collection();
/**
* Set the list of discovery spots
*
* @param IAutoDiscoverySpot[] $autoDiscoverySpots The list of discovery spots
*
* @return $this
*/
public function setAutoDiscoverySpots (array $autoDiscoverySpots): self {
$this->autoDiscoverySpots = $autoDiscoverySpots;
return $this;
} }
/** return $this->autoDiscoverySpots;
* Add a discovery spot
*
* @param IAutoDiscoverySpot $spot The discovery spot
* @param IAutoDiscoverySpot ...$extraSpots Extra discovery spots to add
*
* @return $this
*/
public function addAutoDiscoverySpots (IAutoDiscoverySpot $spot, IAutoDiscoverySpot ...$extraSpots): self {
$this->autoDiscoverySpots[] = $spot;
foreach ($extraSpots as $extraSpot) {
$this->autoDiscoverySpots[] = $extraSpot;
}
return $this;
} }
/** /**

@ -2,6 +2,8 @@
namespace jrosset\CliProgram; namespace jrosset\CliProgram;
use jrosset\Collections\Collection;
use jrosset\Collections\ICollection;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
/** /**
@ -9,43 +11,20 @@ use Symfony\Component\Console\Command\Command;
*/ */
trait TAutoPrefixManagement { trait TAutoPrefixManagement {
/** /**
* @var IAutoPrefixManager[] The lists of manager for commands auto prefix * @var ICollection<IAutoPrefixManager> The lists of manager for commands auto prefix
*/ */
private array $autoPrefixManagers; private ICollection $autoPrefixManagers;
/** /**
* The lists of manager for commands auto prefix * The lists of manager for commands auto prefix
* *
* @return IAutoPrefixManager[] The lists of manager for commands auto prefix * @return ICollection<IAutoPrefixManager> The lists of manager for commands auto prefix
*/ */
public function getAutoPrefixManagers (): array { public function getAutoPrefixManagers (): ICollection {
return $this->autoPrefixManagers; if (!isset($this->autoPrefixManagers)) {
} $this->autoPrefixManagers = new Collection();
/**
* Set The lists of manager for commands auto prefix
*
* @param IAutoPrefixManager[] $autoPrefixManagers The lists of manager for commands auto prefix
*
* @return $this
*/
public function setAutoPrefixManagers (array $autoPrefixManagers): self {
$this->autoPrefixManagers = $autoPrefixManagers;
return $this;
} }
/** return $this->autoPrefixManagers;
* Add a manager for commands auto prefix
*
* @param IAutoPrefixManager $manager The manager
* @param IAutoPrefixManager ...$extraManagers Extra managers to add
*
* @return $this
*/
public function addAutoPrefixManagers (IAutoPrefixManager $manager, IAutoPrefixManager ...$extraManagers): self {
$this->autoPrefixManagers[] = $manager;
foreach ($extraManagers as $extraNamespace) {
$this->autoPrefixManagers[] = $extraNamespace;
}
return $this;
} }
/** /**

@ -12,10 +12,10 @@ class Application extends \Symfony\Component\Console\Application {
public function __construct (string $name = 'UNKNOWN', string $version = 'UNKNOWN') { public function __construct (string $name = 'UNKNOWN', string $version = 'UNKNOWN') {
parent::__construct($name, $version); parent::__construct($name, $version);
$this->addAutoDiscoverySpots( $spot = new AutoDiscoveryDirectory(__DIR__ . '/Commands');
(new AutoDiscoveryDirectory(__DIR__ . '/Commands')) $spot->getAutoPrefixManagers()->prepend(new AutoPrefixNamespaceManager('\\jrosset\\Tests\\Commands', 'test'));
->addAutoPrefixManagers(new AutoPrefixNamespaceManager('\\jrosset\\Tests\\Commands', 'test'))
); $this->getAutoDiscoverySpots()->prepend($spot);
$this->addAutoDiscoveredCommands(); $this->addAutoDiscoveredCommands();
} }
} }
Loading…
Cancel
Save