diff --git a/system/Commands/Utilities/Publish.php b/system/Commands/Utilities/Publish.php index 3e19685cba81..13ffa96eafc6 100644 --- a/system/Commands/Utilities/Publish.php +++ b/system/Commands/Utilities/Publish.php @@ -67,17 +67,24 @@ class Publish extends BaseCommand * * @var array */ - protected $options = []; + protected $options = [ + '--namespace' => 'The namespace from which to search for files to publish. By default, all namespaces are analysed.', + ]; /** * Displays the help for the spark cli script itself. */ public function run(array $params) { - $directory = array_shift($params) ?? 'Publishers'; + $directory = $params[0] ?? 'Publishers'; + $namespace = $params['namespace'] ?? ''; - if ([] === $publishers = Publisher::discover($directory)) { - CLI::write(lang('Publisher.publishMissing', [$directory])); + if ([] === $publishers = Publisher::discover($directory, $namespace)) { + if ($namespace === '') { + CLI::write(lang('Publisher.publishMissing', [$directory])); + } else { + CLI::write(lang('Publisher.publishMissingNamespace', [$directory, $namespace])); + } return; } diff --git a/system/Language/en/Publisher.php b/system/Language/en/Publisher.php index eb18dfebd555..c2d6d51ec900 100644 --- a/system/Language/en/Publisher.php +++ b/system/Language/en/Publisher.php @@ -18,7 +18,8 @@ 'fileNotAllowed' => '"{0}" fails the following restriction for "{1}": {2}', // Publish Command - 'publishMissing' => 'No Publisher classes detected in {0} across all namespaces.', - 'publishSuccess' => '"{0}" published {1} file(s) to "{2}".', - 'publishFailure' => '"{0}" failed to publish to "{1}".', + 'publishMissing' => 'No Publisher classes detected in {0} across all namespaces.', + 'publishMissingNamespace' => 'No Publisher classes detected in {0} in the {1} namespace.', + 'publishSuccess' => '"{0}" published {1} file(s) to "{2}".', + 'publishFailure' => '"{0}" failed to publish to "{1}".', ]; diff --git a/system/Publisher/Publisher.php b/system/Publisher/Publisher.php index 90c966623b2a..62b4f54916b7 100644 --- a/system/Publisher/Publisher.php +++ b/system/Publisher/Publisher.php @@ -99,18 +99,24 @@ class Publisher extends FileCollection * * @return list */ - final public static function discover(string $directory = 'Publishers'): array + final public static function discover(string $directory = 'Publishers', string $namespace = ''): array { - if (isset(self::$discovered[$directory])) { - return self::$discovered[$directory]; + $key = implode('.', [$namespace, $directory]); + + if (isset(self::$discovered[$key])) { + return self::$discovered[$key]; } - self::$discovered[$directory] = []; + self::$discovered[$key] = []; /** @var FileLocatorInterface $locator */ $locator = service('locator'); - if ([] === $files = $locator->listFiles($directory)) { + $files = $namespace === '' + ? $locator->listFiles($directory) + : $locator->listNamespaceFiles($namespace, $directory); + + if ([] === $files) { return []; } @@ -119,13 +125,13 @@ final public static function discover(string $directory = 'Publishers'): array $className = $locator->findQualifiedNameFromPath($file); if ($className !== false && class_exists($className) && is_a($className, self::class, true)) { - self::$discovered[$directory][] = new $className(); + self::$discovered[$key][] = new $className(); } } - sort(self::$discovered[$directory]); + sort(self::$discovered[$key]); - return self::$discovered[$directory]; + return self::$discovered[$key]; } /** diff --git a/tests/system/Publisher/PublisherSupportTest.php b/tests/system/Publisher/PublisherSupportTest.php index 5854e1c516d9..e92bdd0c69c3 100644 --- a/tests/system/Publisher/PublisherSupportTest.php +++ b/tests/system/Publisher/PublisherSupportTest.php @@ -61,6 +61,20 @@ public function testDiscoverNothing(): void $this->assertSame([], $result); } + public function testDiscoverInNamespace(): void + { + $result = Publisher::discover('Publishers', 'Tests\Support'); + $this->assertCount(1, $result); + $this->assertInstanceOf(TestPublisher::class, $result[0]); + } + + public function testDiscoverInUnknowNamespace(): void + { + $result = Publisher::discover('Publishers', 'Nothing\App'); + + $this->assertSame([], $result); + } + public function testDiscoverStores(): void { $publisher = Publisher::discover()[0];