Skip to content

Commit

Permalink
Reverts micro-optimization on view events (#44653)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Oct 19, 2022
1 parent 137d079 commit bb2baa1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 80 deletions.
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

0 comments on commit bb2baa1

Please sign in to comment.