Skip to content

Commit

Permalink
[11.x] Optimize events
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Sep 25, 2024
1 parent 09aaeb4 commit 61b4283
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Foundation/Console/OptimizeClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Events\OptimizeClearing;
use Illuminate\Support\Facades\Event;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'optimize:clear')]
Expand All @@ -22,6 +25,12 @@ class OptimizeClearCommand extends Command
*/
protected $description = 'Remove the cached bootstrap files';

public function __construct(
protected Dispatcher $events,
) {
parent::__construct();
}

/**
* Execute the console command.
*
Expand All @@ -40,6 +49,8 @@ public function handle()
'views' => fn () => $this->callSilent('view:clear') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));

$this->events->dispatch(new OptimizeClearing());

$this->newLine();
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Foundation/Console/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Events\Optimizing;
use Illuminate\Support\Facades\Event;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'optimize')]
Expand All @@ -22,6 +25,12 @@ class OptimizeCommand extends Command
*/
protected $description = 'Cache framework bootstrap, configuration, and metadata to increase performance';

public function __construct(
protected Dispatcher $events,
) {
parent::__construct();
}

/**
* Execute the console command.
*
Expand All @@ -38,6 +47,8 @@ public function handle()
'views' => fn () => $this->callSilent('view:cache') == 0,
])->each(fn ($task, $description) => $this->components->task($description, $task));

$this->events->dispatch(new Optimizing());

$this->newLine();
}
}
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Events/OptimizeClearing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Illuminate\Foundation\Events;

class OptimizeClearing {}
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Events/Optimizing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Illuminate\Foundation\Events;

class Optimizing {}
46 changes: 46 additions & 0 deletions tests/Foundation/Console/OptimizeClearCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Foundation\Console;

use Illuminate\Console\Application;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Console\ClosureCommand;
use Illuminate\Foundation\Console\OptimizeClearCommand;
use Illuminate\Foundation\Events\OptimizeClearing;
use Illuminate\Foundation\Events\Optimizing;
use PHPUnit\Framework\TestCase;

class OptimizeClearCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->app = new Application(
new \Illuminate\Foundation\Application(__DIR__),
$this->events = new Dispatcher(),
'testing',
);

$this->app->addCommands([
new OptimizeClearCommand($this->events),
new ClosureCommand('cache:clear', fn() => 0),
new ClosureCommand('clear-compiled', fn() => 0),
new ClosureCommand('config:clear', fn() => 0),
new ClosureCommand('event:clear', fn() => 0),
new ClosureCommand('route:clear', fn() => 0),
new ClosureCommand('view:clear', fn() => 0),
]);
}

public function testCanListenToOptimizingEvent(): void
{
$this->events->listen(function (OptimizeClearing $event) use (&$called) {
$called = true;
});

$this->app->call('optimize:clear');

$this->assertTrue($called);
}
}
43 changes: 43 additions & 0 deletions tests/Foundation/Console/OptimizeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Tests\Foundation\Console;

use Illuminate\Console\Application;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Console\ClosureCommand;
use Illuminate\Foundation\Console\OptimizeCommand;
use Illuminate\Foundation\Events\Optimizing;
use PHPUnit\Framework\TestCase;

class OptimizeCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$this->app = new Application(
new \Illuminate\Foundation\Application(__DIR__),
$this->events = new Dispatcher(),
'testing',
);

$this->app->addCommands([
new OptimizeCommand($this->events),
new ClosureCommand('config:cache', fn() => 0),
new ClosureCommand('event:cache', fn() => 0),
new ClosureCommand('route:cache', fn() => 0),
new ClosureCommand('view:cache', fn() => 0),
]);
}

public function testCanListenToOptimizingEvent(): void
{
$this->events->listen(function (Optimizing $event) use (&$called) {
$called = true;
});

$this->app->call('optimize');

$this->assertTrue($called);
}
}

0 comments on commit 61b4283

Please sign in to comment.