From 0e507804b143fa796a864245f4bfe019c7869d67 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Sat, 22 Feb 2020 22:56:37 +0100 Subject: [PATCH] human friendly name for bundles (#1491) --- src/Controller/PermissionController.php | 28 ++++++++-------- src/Plugin/Plugin.php | 43 +++++++++++-------------- src/Plugin/PluginManager.php | 5 +++ templates/plugin/index.html.twig | 8 ++++- tests/Command/PluginCommandTest.php | 4 +-- tests/Plugin/PluginManagerTest.php | 2 ++ tests/Plugin/PluginTest.php | 3 ++ 7 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/Controller/PermissionController.php b/src/Controller/PermissionController.php index ba7f4711..3644ca61 100644 --- a/src/Controller/PermissionController.php +++ b/src/Controller/PermissionController.php @@ -80,23 +80,23 @@ final class PermissionController extends AbstractController // be careful, the order of the search keys is important! $permissionOrder = [ - new PermissionSection('User', '_user'), - new PermissionSection('User profile (own)', '_own_profile'), - new PermissionSection('User profile (other)', '_other_profile'), - new PermissionSection('Customer (Teamlead)', '_teamlead_customer'), - new PermissionSection('Customer (Team member)', '_team_customer'), - new PermissionSection('Customer (Admin)', '_customer'), - new PermissionSection('Project (Teamlead)', '_teamlead_project'), - new PermissionSection('Project (Team member)', '_team_project'), - new PermissionSection('Project (Admin)', '_project'), - new PermissionSection('Activity', '_activity'), - new PermissionSection('Timesheet (own)', '_own_timesheet'), - new PermissionSection('Timesheet (other)', '_other_timesheet'), - new PermissionSection('Timesheet', '_timesheet'), new PermissionSection('Export', '_export'), new PermissionSection('Invoice', '_invoice'), new PermissionSection('Teams', '_team'), new PermissionSection('Tags', '_tag'), + new PermissionSection('User profile (other)', '_other_profile'), + new PermissionSection('User profile (own)', '_own_profile'), + new PermissionSection('User', '_user'), + new PermissionSection('Customer (Admin)', '_customer'), + new PermissionSection('Customer (Team member)', '_team_customer'), + new PermissionSection('Customer (Teamlead)', '_teamlead_customer'), + new PermissionSection('Project (Admin)', '_project'), + new PermissionSection('Project (Team member)', '_team_project'), + new PermissionSection('Project (Teamlead)', '_teamlead_project'), + new PermissionSection('Activity', '_activity'), + new PermissionSection('Timesheet', '_timesheet'), + new PermissionSection('Timesheet (other)', '_other_timesheet'), + new PermissionSection('Timesheet (own)', '_own_timesheet'), ]; $event = new PermissionSectionsEvent(); @@ -115,7 +115,7 @@ final class PermissionController extends AbstractController foreach ($this->manager->getPermissions() as $permission) { $found = false; - foreach ($event->getSections() as $section) { + foreach (array_reverse($event->getSections()) as $section) { if ($section->filter($permission)) { $permissionSorted[$section->getTitle()][] = $permission; $found = true; diff --git a/src/Plugin/Plugin.php b/src/Plugin/Plugin.php index e445461b..cd94e389 100644 --- a/src/Plugin/Plugin.php +++ b/src/Plugin/Plugin.php @@ -11,6 +11,10 @@ namespace App\Plugin; class Plugin { + /** + * @var string + */ + private $id; /** * @var string */ @@ -24,60 +28,51 @@ class Plugin */ private $metadata; - /** - * @return PluginMetadata - */ public function getMetadata(): ?PluginMetadata { return $this->metadata; } - /** - * @param PluginMetadata $metadata - * @return Plugin - */ - public function setMetadata(PluginMetadata $metadata) + public function setMetadata(PluginMetadata $metadata): Plugin { $this->metadata = $metadata; return $this; } - /** - * @return string - */ public function getPath(): ?string { return $this->path; } - /** - * @param string $path - * @return Plugin - */ - public function setPath(string $path) + public function setPath(string $path): Plugin { $this->path = $path; return $this; } - /** - * @return string - */ public function getName(): ?string { return $this->name; } - /** - * @param string $name - * @return Plugin - */ - public function setName(string $name) + public function setName(string $name): Plugin { $this->name = $name; return $this; } + + public function getId(): ?string + { + return $this->id; + } + + public function setId(string $id): Plugin + { + $this->id = $id; + + return $this; + } } diff --git a/src/Plugin/PluginManager.php b/src/Plugin/PluginManager.php index ac0c52d5..2719521b 100644 --- a/src/Plugin/PluginManager.php +++ b/src/Plugin/PluginManager.php @@ -71,6 +71,7 @@ class PluginManager { $plugin = new Plugin(); $plugin + ->setId($bundle->getName()) ->setName($bundle->getName()) ->setPath($bundle->getPath()) ->setMetadata(new PluginMetadata()) @@ -100,6 +101,10 @@ class PluginManager $homepage = $json['homepage'] ?? Constants::HOMEPAGE . '/store/'; + if (array_key_exists('name', $json['extra']['kimai'])) { + $plugin->setName($json['extra']['kimai']['name']); + } + $plugin ->getMetadata() ->setHomepage($homepage) diff --git a/templates/plugin/index.html.twig b/templates/plugin/index.html.twig index a79879ea..ddead297 100644 --- a/templates/plugin/index.html.twig +++ b/templates/plugin/index.html.twig @@ -26,7 +26,13 @@ {{ tables.datatable_header(tableName, columns, null, {}) }} {% for plugin in plugins %} - {{ plugin.name }} + + {% if plugin.id != plugin.name %} + {{ plugin.name }} + {% else %} + {{ plugin.name }} + {% endif %} + {{ widgets.label(plugin.metadata.version, 'primary') }} {{ plugin.metadata.description }} diff --git a/tests/Command/PluginCommandTest.php b/tests/Command/PluginCommandTest.php index 42f44255..4dfaa171 100644 --- a/tests/Command/PluginCommandTest.php +++ b/tests/Command/PluginCommandTest.php @@ -30,11 +30,11 @@ class PluginCommandTest extends KernelTestCase public function testWithPlugins() { $plugin1 = $this->getMockBuilder(PluginInterface::class)->onlyMethods(['getName', 'getPath'])->getMock(); - $plugin1->expects($this->exactly(3))->method('getName')->willReturn('Test-Bundle'); + $plugin1->expects($this->any())->method('getName')->willReturn('Test-Bundle'); $plugin1->expects($this->once())->method('getPath')->willReturn(__DIR__); $plugin2 = $this->getMockBuilder(PluginInterface::class)->onlyMethods(['getName', 'getPath'])->getMock(); - $plugin2->expects($this->exactly(3))->method('getName')->willReturn('Another one'); + $plugin2->expects($this->any())->method('getName')->willReturn('Another one'); $plugin2->expects($this->once())->method('getPath')->willReturn('BundleDirectory'); $commandTester = $this->getCommandTester([$plugin1, $plugin2], []); diff --git a/tests/Plugin/PluginManagerTest.php b/tests/Plugin/PluginManagerTest.php index 46f6e6b1..bb0a58c0 100644 --- a/tests/Plugin/PluginManagerTest.php +++ b/tests/Plugin/PluginManagerTest.php @@ -72,6 +72,8 @@ class PluginManagerTest extends TestCase $meta = $plugin->getMetadata(); $this->assertEquals('0.9', $meta->getKimaiVersion()); $this->assertEquals('1.0', $meta->getVersion()); + $this->assertEquals('TestPlugin', $plugin->getId()); + $this->assertEquals('TestPlugin from composer.json', $plugin->getName()); $this->assertEquals('Just a test fixture for the PluginManager', $meta->getDescription()); $this->assertEquals('https://github.com/kevinpapst/kimai2', $meta->getHomepage()); } diff --git a/tests/Plugin/PluginTest.php b/tests/Plugin/PluginTest.php index 5b62492b..7f7766c2 100644 --- a/tests/Plugin/PluginTest.php +++ b/tests/Plugin/PluginTest.php @@ -21,6 +21,7 @@ class PluginTest extends TestCase public function testEmptyObject() { $plugin = new Plugin(); + $this->assertNull($plugin->getId()); $this->assertNull($plugin->getName()); $this->assertNull($plugin->getPath()); $this->assertNull($plugin->getMetadata()); @@ -37,10 +38,12 @@ class PluginTest extends TestCase ; $plugin = new Plugin(); + $this->assertInstanceOf(Plugin::class, $plugin->setId('foo2')); $this->assertInstanceOf(Plugin::class, $plugin->setName('foo')); $this->assertInstanceOf(Plugin::class, $plugin->setPath('bar')); $this->assertInstanceOf(Plugin::class, $plugin->setMetadata($metadata)); + $this->assertEquals('foo2', $plugin->getId()); $this->assertEquals('foo', $plugin->getName()); $this->assertEquals('bar', $plugin->getPath()); $this->assertSame($metadata, $plugin->getMetadata());