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

[9.x] Reverts micro-optimization on view events #44653

Merged
merged 1 commit into from
Oct 19, 2022
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
51 changes: 2 additions & 49 deletions src/Illuminate/View/Concerns/ManagesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,10 @@

use Closure;
use Illuminate\Contracts\View\View as ViewContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait ManagesEvents
{
/**
* An array of views and whether they have registered "creators".
*
* @var array<string, true>|true
*/
protected $shouldCallCreators = [];

/**
* An array of views and whether they have registered "composers".
*
* @var array<string, true>|true
*/
protected $shouldCallComposers = [];

/**
* Register a view creator event.
*
Expand All @@ -32,18 +17,6 @@ trait ManagesEvents
*/
public function creator($views, $callback)
{
if (is_array($this->shouldCallCreators)) {
foreach (Arr::wrap($views) as $view) {
if (str_contains($view, '*')) {
$this->shouldCallCreators = true;

break;
}

$this->shouldCallCreators[$this->normalizeName($view)] = true;
}
}

$creators = [];

foreach ((array) $views as $view) {
Expand Down Expand Up @@ -79,18 +52,6 @@ public function composers(array $composers)
*/
public function composer($views, $callback)
{
if (is_array($this->shouldCallComposers)) {
foreach (Arr::wrap($views) as $view) {
if (str_contains($view, '*')) {
$this->shouldCallComposers = true;

break;
}

$this->shouldCallComposers[$this->normalizeName($view)] = true;
}
}

$composers = [];

foreach ((array) $views as $view) {
Expand Down Expand Up @@ -213,11 +174,7 @@ protected function addEventListener($name, $callback)
*/
public function callComposer(ViewContract $view)
{
if ($this->shouldCallComposers === true || isset($this->shouldCallComposers[
$this->normalizeName($view->name())
])) {
$this->events->dispatch('composing: '.$view->name(), [$view]);
}
$this->events->dispatch('composing: '.$view->name(), [$view]);
}

/**
Expand All @@ -228,10 +185,6 @@ public function callComposer(ViewContract $view)
*/
public function callCreator(ViewContract $view)
{
if ($this->shouldCallCreators === true || isset($this->shouldCallCreators[
$this->normalizeName((string) $view->name())
])) {
$this->events->dispatch('creating: '.$view->name(), [$view]);
}
$this->events->dispatch('creating: '.$view->name(), [$view]);
}
}
40 changes: 9 additions & 31 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,6 @@ public function testPrependedExtensionOverridesExistingExtensions()
$this->assertSame('baz', key($extensions));
}

public function testCallCreatorsDoesNotDispatchEventsWhenIsNotNecessary()
{
$factory = $this->getFactory();
$factory->getDispatcher()->shouldReceive('listen')->never();
$factory->getDispatcher()->shouldReceive('dispatch')->never();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('name');

$factory->callCreator($view);
}

public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
{
$factory = $this->getFactory();
Expand All @@ -208,7 +196,7 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('name');
$view->shouldReceive('name')->once()->andReturn('name');

$factory->creator('name', fn () => true);

Expand Down Expand Up @@ -297,25 +285,15 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNormalized
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('components/button');
$view->shouldReceive('name')
->once()
->andReturn('components/button');

$factory->creator('components.button', fn () => true);

$factory->callCreator($view);
}

public function testCallComposersDoesNotDispatchEventsWhenIsNotNecessary()
{
$factory = $this->getFactory();
$factory->getDispatcher()->shouldReceive('listen')->never();
$factory->getDispatcher()->shouldReceive('dispatch')->never();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('name');

$factory->callComposer($view);
}

public function testCallComposerDoesDispatchEventsWhenIsNecessary()
{
$factory = $this->getFactory();
Expand All @@ -330,7 +308,7 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessary()
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('name');
$view->shouldReceive('name')->once()->andReturn('name');

$factory->composer('name', fn () => true);

Expand All @@ -351,7 +329,7 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArra
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('name');
$view->shouldReceive('name')->once()->andReturn('name');

$factory->composer(['name'], fn () => true);

Expand Down Expand Up @@ -440,7 +418,7 @@ public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNormalize
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('components/button');
$view->shouldReceive('name')->once()->andReturn('components/button');

$factory->composer('components.button', fn () => true);

Expand Down Expand Up @@ -514,7 +492,7 @@ public function testCallComposerCallsProperEvent()

$dispatcher->shouldReceive('listen', m::any())->once();

$view->shouldReceive('name')->twice()->andReturn('name');
$view->shouldReceive('name')->once()->andReturn('name');

$factory->composer('name', fn () => true);

Expand Down Expand Up @@ -862,7 +840,7 @@ public function testExceptionsInSectionsAreThrown()
$factory->getEngineResolver()->shouldReceive('resolve')->twice()->andReturn($engine);
$factory->getFinder()->shouldReceive('find')->once()->with('layout')->andReturn(__DIR__.'/fixtures/section-exception-layout.php');
$factory->getFinder()->shouldReceive('find')->once()->with('view')->andReturn(__DIR__.'/fixtures/section-exception.php');
$factory->getDispatcher()->shouldReceive('dispatch')->never();
$factory->getDispatcher()->shouldReceive('dispatch')->times(4); // 2 "creating" + 2 "composing"...

$factory->make('view')->render();
}
Expand Down