Generate the sidebar event handler on module scaffold

This is replacing the SidebarExtender class. Improves overall performance.
parent d5a81cfd
......@@ -39,7 +39,7 @@ class EntityGenerator extends Generator
$entityTypeStub = "entity-{$entityType}.stub";
if ($regenerateSidebar === true) {
$this->generateSidebarExtender($entities);
$this->generateSidebarListener($entities);
}
foreach ($entities as $entity) {
......@@ -213,11 +213,11 @@ class EntityGenerator extends Generator
*/
private function appendSidebarLinksFor($entity)
{
$sidebarComposerContent = $this->finder->get($this->getModulesPath('Sidebar/SidebarExtender.php'));
$sidebarComposerContent = $this->finder->get($this->getModulesPath("Events/Handlers/Register{$this->name}Sidebar.php"));
$content = $this->getContentForStub('append-sidebar-extender.stub', $entity);
$sidebarComposerContent = str_replace('// append', $content, $sidebarComposerContent);
$this->finder->put($this->getModulesPath('Sidebar/SidebarExtender.php'), $sidebarComposerContent);
$this->finder->put($this->getModulesPath("Events/Handlers/Register{$this->name}Sidebar.php"), $sidebarComposerContent);
}
/**
......@@ -242,6 +242,27 @@ class EntityGenerator extends Generator
);
}
/**
* Generate a sidebar event listener
* @param $entities
*/
public function generateSidebarListener($entities)
{
$name = "Register{$this->name}Sidebar";
if (count($entities) > 0) {
return $this->writeFile(
$this->getModulesPath("Events/Handlers/$name"),
$this->getContentForStub('sidebar-listener.stub', $name)
);
}
return $this->writeFile(
$this->getModulesPath("Events/Handlers/$name"),
$this->getContentForStub('sidebar-listener-empty.stub', $name)
);
}
/**
* Get the current time with microseconds
* @return string
......
......@@ -48,8 +48,20 @@ class FilesGenerator extends Generator
$stub = $this->finder->get($this->getStubPath($stub));
return str_replace(
['$MODULE$', '$LOWERCASE_MODULE$', '$PLURAL_MODULE$', '$UPPERCASE_PLURAL_MODULE$'],
[$this->name, strtolower($this->name), strtolower(str_plural($this->name)), str_plural($this->name)],
[
'$MODULE$',
'$LOWERCASE_MODULE$',
'$PLURAL_MODULE$',
'$UPPERCASE_PLURAL_MODULE$',
'$SIDEBAR_LISTENER_NAME$',
],
[
$this->name,
strtolower($this->name),
strtolower(str_plural($this->name)),
str_plural($this->name),
"Register{$this->name}Sidebar"
],
$stub
);
}
......
......@@ -264,6 +264,27 @@ class ModuleScaffoldTest extends BaseTestCase
$this->cleanUp();
}
/** @test */
public function it_generates_service_provider_with_content()
{
$this->scaffoldModuleWithEloquent();
$file = $this->finder->get($this->testModulePath . "/Providers/{$this->testModuleName}ServiceProvider.php");
$sidebarEventListenerName = "Register{$this->testModuleName}Sidebar";
$this->assertTrue(str_contains(
$file,
'$this->loadMigrationsFrom(__DIR__ . \'/../Database/Migrations\');'
), 'Migrations arent loaded');
$this->assertTrue(str_contains(
$file,
'$this->app[\'events\']->listen(BuildingSidebar::class, '. $sidebarEventListenerName . '::class);'
), 'Sidebar event handler was not present');
$this->cleanUp();
}
/** @test */
public function it_should_generate_controllers()
{
......@@ -291,36 +312,41 @@ class ModuleScaffoldTest extends BaseTestCase
}
/** @test */
public function it_should_generate_sidebar_extender_file()
public function it_should_generate_sidebar_event_handler()
{
$this->scaffoldModuleWithEloquent();
$file1 = $this->finder->isFile($this->testModulePath . '/Sidebar/SidebarExtender.php');
$file = $this->finder->isFile($this->testModulePath . '/Events/Handlers/RegisterTestingTestModuleSidebar.php');
$this->assertTrue($file1);
$this->assertTrue($file);
$this->cleanUp();
}
/** @test */
public function it_should_generate_filled_sidebar_extender()
public function it_should_generate_filled_sidebar_event_handler()
{
$this->scaffoldModuleWithEloquent();
$viewComposer = $this->finder->get($this->testModulePath . '/Sidebar/SidebarExtender.php');
$file = $this->finder->get($this->testModulePath . '/Events/Handlers/RegisterTestingTestModuleSidebar.php');
$this->assertTrue(str_contains($file, '$menu->group'));
$this->assertTrue(str_contains($file, 'class RegisterTestingTestModuleSidebar'));
$this->assertTrue(str_contains($viewComposer, '$menu->group'));
$this->cleanUp();
}
/** @test */
public function it_should_generate_empty_sidebar_extender_if_no_entities()
public function it_should_generate_empty_sidebar_event_handler_if_no_entities()
{
$this->scaffoldModule('Eloquent', [], []);
$viewComposer = $this->finder->get($this->testModulePath . '/Sidebar/SidebarExtender.php');
$file = $this->finder->get($this->testModulePath . '/Events/Handlers/RegisterTestingTestModuleSidebar.php');
$this->assertFalse(str_contains($file, '$menu->group'));
$this->assertTrue(str_contains($file, 'return $menu'));
$this->assertFalse(str_contains($viewComposer, '$menu->group'));
$this->assertTrue(str_contains($viewComposer, 'return $menu'));
$this->cleanUp();
}
/** @test */
......
......@@ -4,6 +4,9 @@ versions:
changed:
- Using the @push js stacks over the scripts section
- Using the @push css stacks over the styles section
- Register module migrations by default to ease testing in service provider
- SidebarExtender class has been removed in favor of new Sidebar event handlers
- Register the sidebar event handler in service provider
removed:
- Removing ckeditor from the default scaffolded views, now included via <code>EditorIsRendering</code> hook
"2.4.0":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment