Skip to content

Commit

Permalink
Allow for the collection of stubs to be published (#45653)
Browse files Browse the repository at this point in the history
* Add event for collecting publishable stubs

* Style CI fixes

* Removed empty line from CollectStubsForPublishing::addStub docblock

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
ollieread and taylorotwell authored Jan 16, 2023
1 parent 31fa534 commit 955db51
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 45 deletions.
95 changes: 50 additions & 45 deletions src/Illuminate/Foundation/Console/StubPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Events\PublishingStubs;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'stub:publish')]
Expand Down Expand Up @@ -47,53 +48,57 @@ public function handle()
(new Filesystem)->makeDirectory($stubsPath);
}

$files = [
__DIR__.'/stubs/cast.inbound.stub' => $stubsPath.'/cast.inbound.stub',
__DIR__.'/stubs/cast.stub' => $stubsPath.'/cast.stub',
__DIR__.'/stubs/console.stub' => $stubsPath.'/console.stub',
__DIR__.'/stubs/event.stub' => $stubsPath.'/event.stub',
__DIR__.'/stubs/job.queued.stub' => $stubsPath.'/job.queued.stub',
__DIR__.'/stubs/job.stub' => $stubsPath.'/job.stub',
__DIR__.'/stubs/mail.stub' => $stubsPath.'/mail.stub',
__DIR__.'/stubs/markdown-mail.stub' => $stubsPath.'/markdown-mail.stub',
__DIR__.'/stubs/markdown-notification.stub' => $stubsPath.'/markdown-notification.stub',
__DIR__.'/stubs/model.pivot.stub' => $stubsPath.'/model.pivot.stub',
__DIR__.'/stubs/model.stub' => $stubsPath.'/model.stub',
__DIR__.'/stubs/notification.stub' => $stubsPath.'/notification.stub',
__DIR__.'/stubs/observer.plain.stub' => $stubsPath.'/observer.plain.stub',
__DIR__.'/stubs/observer.stub' => $stubsPath.'/observer.stub',
__DIR__.'/stubs/policy.plain.stub' => $stubsPath.'/policy.plain.stub',
__DIR__.'/stubs/policy.stub' => $stubsPath.'/policy.stub',
__DIR__.'/stubs/provider.stub' => $stubsPath.'/provider.stub',
__DIR__.'/stubs/request.stub' => $stubsPath.'/request.stub',
__DIR__.'/stubs/resource.stub' => $stubsPath.'/resource.stub',
__DIR__.'/stubs/resource-collection.stub' => $stubsPath.'/resource-collection.stub',
__DIR__.'/stubs/rule.stub' => $stubsPath.'/rule.stub',
__DIR__.'/stubs/scope.stub' => $stubsPath.'/scope.stub',
__DIR__.'/stubs/test.stub' => $stubsPath.'/test.stub',
__DIR__.'/stubs/test.unit.stub' => $stubsPath.'/test.unit.stub',
__DIR__.'/stubs/view-component.stub' => $stubsPath.'/view-component.stub',
realpath(__DIR__.'/../../Database/Console/Factories/stubs/factory.stub') => $stubsPath.'/factory.stub',
realpath(__DIR__.'/../../Database/Console/Seeds/stubs/seeder.stub') => $stubsPath.'/seeder.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.create.stub') => $stubsPath.'/migration.create.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.stub') => $stubsPath.'/migration.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.update.stub') => $stubsPath.'/migration.update.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.api.stub') => $stubsPath.'/controller.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.invokable.stub') => $stubsPath.'/controller.invokable.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.api.stub') => $stubsPath.'/controller.model.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.stub') => $stubsPath.'/controller.model.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.api.stub') => $stubsPath.'/controller.nested.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.api.stub') => $stubsPath.'/controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.stub') => $stubsPath.'/controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.stub') => $stubsPath.'/controller.nested.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.plain.stub') => $stubsPath.'/controller.plain.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.api.stub') => $stubsPath.'/controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.stub') => $stubsPath.'/controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.stub') => $stubsPath.'/controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/middleware.stub') => $stubsPath.'/middleware.stub',
$stubs = [
__DIR__.'/stubs/cast.inbound.stub' => 'cast.inbound.stub',
__DIR__.'/stubs/cast.stub' => 'cast.stub',
__DIR__.'/stubs/console.stub' => 'console.stub',
__DIR__.'/stubs/event.stub' => 'event.stub',
__DIR__.'/stubs/job.queued.stub' => 'job.queued.stub',
__DIR__.'/stubs/job.stub' => 'job.stub',
__DIR__.'/stubs/mail.stub' => 'mail.stub',
__DIR__.'/stubs/markdown-mail.stub' => 'markdown-mail.stub',
__DIR__.'/stubs/markdown-notification.stub' => 'markdown-notification.stub',
__DIR__.'/stubs/model.pivot.stub' => 'model.pivot.stub',
__DIR__.'/stubs/model.stub' => 'model.stub',
__DIR__.'/stubs/notification.stub' => 'notification.stub',
__DIR__.'/stubs/observer.plain.stub' => 'observer.plain.stub',
__DIR__.'/stubs/observer.stub' => 'observer.stub',
__DIR__.'/stubs/policy.plain.stub' => 'policy.plain.stub',
__DIR__.'/stubs/policy.stub' => 'policy.stub',
__DIR__.'/stubs/provider.stub' => 'provider.stub',
__DIR__.'/stubs/request.stub' => 'request.stub',
__DIR__.'/stubs/resource.stub' => 'resource.stub',
__DIR__.'/stubs/resource-collection.stub' => 'resource-collection.stub',
__DIR__.'/stubs/rule.stub' => 'rule.stub',
__DIR__.'/stubs/scope.stub' => 'scope.stub',
__DIR__.'/stubs/test.stub' => 'test.stub',
__DIR__.'/stubs/test.unit.stub' => 'test.unit.stub',
__DIR__.'/stubs/view-component.stub' => 'view-component.stub',
realpath(__DIR__.'/../../Database/Console/Factories/stubs/factory.stub') => 'factory.stub',
realpath(__DIR__.'/../../Database/Console/Seeds/stubs/seeder.stub') => 'seeder.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.create.stub') => 'migration.create.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.stub') => 'migration.stub',
realpath(__DIR__.'/../../Database/Migrations/stubs/migration.update.stub') => 'migration.update.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.api.stub') => 'controller.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.invokable.stub') => 'controller.invokable.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.api.stub') => 'controller.model.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.model.stub') => 'controller.model.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.api.stub') => 'controller.nested.api.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.api.stub') => 'controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.singleton.stub') => 'controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.nested.stub') => 'controller.nested.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.plain.stub') => 'controller.plain.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.api.stub') => 'controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.singleton.stub') => 'controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/controller.stub') => 'controller.stub',
realpath(__DIR__.'/../../Routing/Console/stubs/middleware.stub') => 'middleware.stub',
];

foreach ($files as $from => $to) {
$this->laravel['events']->dispatch($event = new PublishingStubs($stubs));

foreach ($event->stubs as $from => $to) {
$to = $stubsPath.DIRECTORY_SEPARATOR.ltrim($to, DIRECTORY_SEPARATOR);

if ((! $this->option('existing') && (! file_exists($to) || $this->option('force')))
|| ($this->option('existing') && file_exists($to))) {
file_put_contents($to, file_get_contents($from));
Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Foundation/Events/PublishingStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Foundation\Events;

class PublishingStubs
{
use Dispatchable;

/**
* The stubs being published.
*
* @var array
*/
public $stubs = [];

/**
* Create a new event instance.
*
* @param array $stubs
* @return void
*/
public function __construct(array $stubs)
{
$this->stubs = $stubs;
}

/**
* Add a new stub to be published.
*
* @param string $path
* @param string $name
* @return $this
*/
public function add(string $path, string $name)
{
$this->stubs[$path] = $name;

return $this;
}
}

0 comments on commit 955db51

Please sign in to comment.