Skip to content

Commit bcedf1c

Browse files
authored
feat: added the namespace option to the publish command (#9278)
1 parent d6d30c0 commit bcedf1c

File tree

7 files changed

+78
-15
lines changed

7 files changed

+78
-15
lines changed

system/Commands/Utilities/Publish.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,24 @@ class Publish extends BaseCommand
6767
*
6868
* @var array<string, string>
6969
*/
70-
protected $options = [];
70+
protected $options = [
71+
'--namespace' => 'The namespace from which to search for files to publish. By default, all namespaces are analysed.',
72+
];
7173

7274
/**
7375
* Displays the help for the spark cli script itself.
7476
*/
7577
public function run(array $params)
7678
{
77-
$directory = array_shift($params) ?? 'Publishers';
79+
$directory = $params[0] ?? 'Publishers';
80+
$namespace = $params['namespace'] ?? '';
7881

79-
if ([] === $publishers = Publisher::discover($directory)) {
80-
CLI::write(lang('Publisher.publishMissing', [$directory]));
82+
if ([] === $publishers = Publisher::discover($directory, $namespace)) {
83+
if ($namespace === '') {
84+
CLI::write(lang('Publisher.publishMissing', [$directory]));
85+
} else {
86+
CLI::write(lang('Publisher.publishMissingNamespace', [$directory, $namespace]));
87+
}
8188

8289
return;
8390
}

system/Language/en/Publisher.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
'fileNotAllowed' => '"{0}" fails the following restriction for "{1}": {2}',
1919

2020
// Publish Command
21-
'publishMissing' => 'No Publisher classes detected in {0} across all namespaces.',
22-
'publishSuccess' => '"{0}" published {1} file(s) to "{2}".',
23-
'publishFailure' => '"{0}" failed to publish to "{1}".',
21+
'publishMissing' => 'No Publisher classes detected in {0} across all namespaces.',
22+
'publishMissingNamespace' => 'No Publisher classes detected in {0} in the {1} namespace.',
23+
'publishSuccess' => '"{0}" published {1} file(s) to "{2}".',
24+
'publishFailure' => '"{0}" failed to publish to "{1}".',
2425
];

system/Publisher/Publisher.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,24 @@ class Publisher extends FileCollection
9999
*
100100
* @return list<self>
101101
*/
102-
final public static function discover(string $directory = 'Publishers'): array
102+
final public static function discover(string $directory = 'Publishers', string $namespace = ''): array
103103
{
104-
if (isset(self::$discovered[$directory])) {
105-
return self::$discovered[$directory];
104+
$key = implode('.', [$namespace, $directory]);
105+
106+
if (isset(self::$discovered[$key])) {
107+
return self::$discovered[$key];
106108
}
107109

108-
self::$discovered[$directory] = [];
110+
self::$discovered[$key] = [];
109111

110112
/** @var FileLocatorInterface $locator */
111113
$locator = service('locator');
112114

113-
if ([] === $files = $locator->listFiles($directory)) {
115+
$files = $namespace === ''
116+
? $locator->listFiles($directory)
117+
: $locator->listNamespaceFiles($namespace, $directory);
118+
119+
if ([] === $files) {
114120
return [];
115121
}
116122

@@ -119,13 +125,13 @@ final public static function discover(string $directory = 'Publishers'): array
119125
$className = $locator->findQualifiedNameFromPath($file);
120126

121127
if ($className !== false && class_exists($className) && is_a($className, self::class, true)) {
122-
self::$discovered[$directory][] = new $className();
128+
self::$discovered[$key][] = new $className();
123129
}
124130
}
125131

126-
sort(self::$discovered[$directory]);
132+
sort(self::$discovered[$key]);
127133

128-
return self::$discovered[$directory];
134+
return self::$discovered[$key];
129135
}
130136

131137
/**

tests/system/Publisher/PublisherSupportTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ public function testDiscoverNothing(): void
6161
$this->assertSame([], $result);
6262
}
6363

64+
public function testDiscoverInNamespace(): void
65+
{
66+
$result = Publisher::discover('Publishers', 'Tests\Support');
67+
$this->assertCount(1, $result);
68+
$this->assertInstanceOf(TestPublisher::class, $result[0]);
69+
}
70+
71+
public function testDiscoverInUnknowNamespace(): void
72+
{
73+
$result = Publisher::discover('Publishers', 'Nothing\App');
74+
75+
$this->assertSame([], $result);
76+
}
77+
6478
public function testDiscoverStores(): void
6579
{
6680
$publisher = Publisher::discover()[0];

user_guide_src/source/changelogs/v4.6.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ Removed Deprecated Items
160160
Enhancements
161161
************
162162

163+
Publisher
164+
=========
165+
166+
- ``Publisher::discover()`` now accepts a second parameter (``namespace``) specifying the namespace in which publishers should be searched. See :ref:`discovery-in-a-specific-namespace` for the details.
167+
163168
Exceptions
164169
==========
165170

user_guide_src/source/libraries/publisher.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ Most of the time you will not need to handle your own discovery, just use the pr
7575
By default on your class extension ``publish()`` will add all files from your ``$source`` and merge them
7676
out to your destination, overwriting on collision.
7777

78+
.. _discovery-in-a-specific-namespace:
79+
80+
Discovery in a specific namespace
81+
---------------------------------
82+
83+
.. versionadded:: 4.6.0
84+
85+
Since v4.6.0, you can also scan a specific namespace. This not only reduces the number of files to be scanned,
86+
but also avoids the need to rerun a Publisher. All you need to do is specify the desired root namespace in the
87+
second parameter of the ``discover()`` method.
88+
89+
.. literalinclude:: publisher/016.php
90+
91+
The specified namespace must be known to CodeIgniter. You can check the list of all namespaces using the "spark namespaces" command:
92+
93+
.. code-block:: console
94+
95+
php spark namespaces
96+
97+
The "publish" command also offers the ``--namespace`` option to define the namespace when searching for Publishers that might come from a library.
98+
99+
.. code-block:: console
100+
101+
php spark publish --namespace Namespace\Vendor\Package
102+
78103
Security
79104
========
80105

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use CodeIgniter\Publisher\Publisher;
4+
5+
$memePublishers = Publisher::discover('Publishers', 'Namespace\Vendor\Package');

0 commit comments

Comments
 (0)