Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add prompt to vendor:publish to specify which provider/tag to publish #18230

Merged
merged 3 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 94 additions & 5 deletions src/Illuminate/Foundation/Console/VendorPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ class VendorPublishCommand extends Command
*/
protected $files;

/**
* The provider to publish.
*
* @var string
*/
protected $provider = null;

/**
* The tags to publish.
*
* @var array
*/
protected $tags = [];

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'vendor:publish {--force : Overwrite any existing files.}
{--all : Publish assets for all service providers without prompt.}
{--provider= : The service provider that has assets you want to publish.}
{--tag=* : One or many tags that have assets you want to publish.}';

Expand Down Expand Up @@ -54,11 +69,87 @@ public function __construct(Filesystem $files)
*/
public function fire()
{
$tags = $this->option('tag') ?: [null];
$this->setProviderOrTagsToPublish();

foreach ((array) $tags as $tag) {
$tags = $this->tags ?: [null];

foreach ($tags as $tag) {
$this->publishTag($tag);
}

$this->info('Publishing complete.');
}

/**
* Determine the provider or tag(s) to publish.
*
* @return void
*/
protected function setProviderOrTagsToPublish()
{
if ($this->option('all')) {
return;
}

$this->provider = $this->option('provider');

$this->tags = (array) $this->option('tag');

if ($this->provider || $this->tags) {
return;
}

$this->promptForProviderOrTag();
}

/**
* Prompt for which provider or tag to publish.
*
* @return void
*/
protected function promptForProviderOrTag()
{
$choice = $this->choice(
"Which provider or tag's files would you like to publish?",
$choices = $this->publishableChoices()
);

if ($choice == $choices[0]) {
return;
}

$this->parseChoice($choice);
}

/**
* The choices available via the prompt.
*
* @return array
*/
protected function publishableChoices()
{
return array_merge(
['<comment>Publish files from all providers and tags listed below</comment>'],
preg_filter('/^/', '<comment>Provider: </comment>', ServiceProvider::publishableProviders()),
preg_filter('/^/', '<comment>Tag: </comment>', ServiceProvider::publishableGroups())
);
}

/**
* Parse the answer that was given via the prompt.
*
* @param string $choice
* @return void
*/
protected function parseChoice($choice)
{
list($type, $value) = explode(': ', strip_tags($choice));

if ($type == 'Provider') {
$this->provider = $value;
} elseif ($type == 'Tag') {
$this->tags = [$value];
}
}

/**
Expand All @@ -72,8 +163,6 @@ protected function publishTag($tag)
foreach ($this->pathsToPublish($tag) as $from => $to) {
$this->publishItem($from, $to);
}

$this->info('Publishing complete.');
}

/**
Expand All @@ -85,7 +174,7 @@ protected function publishTag($tag)
protected function pathsToPublish($tag)
{
return ServiceProvider::pathsToPublish(
$this->option('provider'), $tag
$this->provider, $tag
);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ protected function addPublishGroup($group, $paths)
);
}

/**
* Get the service providers available for publishing.
*
* @return array
*/
public static function publishableProviders()
{
return array_keys(static::$publishes);
}

/**
* Get the groups available for publishing.
*
* @return array
*/
public static function publishableGroups()
{
return array_keys(static::$publishGroups);
}

/**
* Get the paths to publish.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/SupportServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public function tearDown()
m::close();
}

public function testPublishableServiceProviders()
{
$toPublish = ServiceProvider::publishableProviders();
$expected = [
'Illuminate\Tests\Support\ServiceProviderForTestingOne',
'Illuminate\Tests\Support\ServiceProviderForTestingTwo',
];
$this->assertEquals($expected, $toPublish, 'Publishable service providers do not return expected set of providers.');
}

public function testPublishableGroups()
{
$toPublish = ServiceProvider::publishableGroups();
$this->assertEquals(['some_tag'], $toPublish, 'Publishable groups do not return expected set of groups.');
}

public function testSimpleAssetsArePublishedCorrectly()
{
$toPublish = ServiceProvider::pathsToPublish('Illuminate\Tests\Support\ServiceProviderForTestingOne');
Expand Down