Skip to content

Commit

Permalink
Merge pull request #39 from marcin-jozwikowski/feature/37-controller-…
Browse files Browse the repository at this point in the history
…option

Feature/37 controller option
  • Loading branch information
marcin-jozwikowski authored Jun 17, 2024
2 parents 1296ea9 + e93ab95 commit 2a6780a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ into

The following parameters are in use:

| Parameter | Default value | Description |
|----------------------|------------------------------------------------------------------------------------------------|------------------------------------------------|
| `route_prefix` | `"pretty"` | First part of route name |
| `default_dashboard` | `"App\Controller\EasyAdmin\DashboardController::index"` | Controller action to invoke |
| `default_actions` | `["index", "new", "detail", "edit", "delete", "batchDelete", "renderFilters", "autocomplete"]` | Default set of actions to build routes against |
| `include_menu_index` | `false` | Should menu index be included in path |
| `drop_entity_fqcn` | `false` | Should `entityFqcn` be removed from the URLs |
| Parameter | Default value | Description |
|----------------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
| `route_prefix` | `"pretty"` | First part of route name |
| `default_dashboard` | `"App\Controller\EasyAdmin\DashboardController::index"` | Default controller action to invoke if not specified in attributes |
| `default_actions` | `["index", "new", "detail", "edit", "delete", "batchDelete", "renderFilters", "autocomplete"]` | Default set of actions to build routes against |
| `include_menu_index` | `false` | Should menu index be included in path |
| `drop_entity_fqcn` | `false` | Should `entityFqcn` be removed from the URLs |

To change the default values set the parameter in your `services.yaml`

Expand Down Expand Up @@ -138,7 +138,7 @@ There are one function, and one filter being registered by a Twig extension in t

* ### Select actions to create routes for

By default pretty routes are generated for `index`, `new`, `detail`, `edit`, `delete`, `batchDelete`, `renderFilters`, and `autocomplete` actions.
By default, pretty routes are generated for `index`, `new`, `detail`, `edit`, `delete`, `batchDelete`, `renderFilters`, and `autocomplete` actions.

To change them globally set a proper [_configuration_](#Configuration) value.
For a single-controller change add a `PrettyRoutesController` attribute to the controller and name the actions you want to have pretty routes for, in `actions` parameter.
Expand All @@ -148,6 +148,15 @@ There are one function, and one filter being registered by a Twig extension in t
class AnyFancyController {
// ...
}
/**
* You can also (optionally) specify your own dashboard controller if you're using more than one in your project.
* This will avoid to have an incorrect sidebar/user menu by addressing the request to the right dashboard controller
*/
#[PrettyRoutesController(actions: ['index', 'foo', 'bar'], dasboard: YourCrudController::class . '::yourCrudAction')]
class AnyFancyController {
// ...
}
```
You can also just add your custom actions to the default list by specifying `customActions` in `PrettyRoutesController` attribute.
Expand All @@ -158,7 +167,7 @@ There are one function, and one filter being registered by a Twig extension in t
```
* ### Define routes manually
Instead of defining a `pretty_routes` routes to automatically parse all classes in a directory you can ceate routes that will replace your default EasyAdmin CRUD actions.
Instead of defining a `pretty_routes` routes to automatically parse all classes in a directory you can create routes that will replace your default EasyAdmin CRUD actions.
```yaml
pretty_foobar_index:
Expand Down Expand Up @@ -189,7 +198,7 @@ There are one function, and one filter being registered by a Twig extension in t
If your routes are still not generated despite being added, check your logs for `'Pretty route not found'` with `debug` level. Those will list all the EasyAdmin routes that did not have their pretty counterparts.
Most probably, there's some naming missmatch.
Most probably, there's some naming mismatch.
* ### Checking the Resource parsing results
Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/PrettyRoutesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PrettyRoutesController
public const ARGUMENT_ACTIONS = 'actions';
public const ARGUMENT_CUSTOM_ACTIONS = 'customActions';
public const ARGUMENT_PATH = 'path';
public const ARGUMENT_DASHBOARD = 'dashboard';
public const DEFAULT_ACTIONS = [
Action::INDEX,
Action::NEW,
Expand All @@ -32,6 +33,7 @@ public function __construct(
public ?array $actions = null,
public ?array $customActions = null,
public ?string $path = null,
public ?string $dashboard = null,
) {
}
}
6 changes: 3 additions & 3 deletions src/Service/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ private function getControllerAttribute(ReflectionClass $reflection): ?Reflectio

private function makeRouteDto(ReflectionClass $reflection, string $action, string $actionPath): ActionRouteDto
{
// determine the first part of the final URL - classname or the value from PrettyRoutesController attribute
$classAttribute = $this->getControllerAttribute($reflection);
$routePathFormat = '/%s/%s';
$routeDefaults = [
// set route values
'_controller' => $this->prettyUrlsDefaultDashboard,
'_controller' => $classAttribute?->getArguments()[PrettyRoutesController::ARGUMENT_DASHBOARD] ?? $this->prettyUrlsDefaultDashboard,
PrettyUrlsGenerator::EA_FQCN => $reflection->getName(),
PrettyUrlsGenerator::EA_ACTION => $action,
];
Expand All @@ -108,8 +110,6 @@ private function makeRouteDto(ReflectionClass $reflection, string $action, strin
}

$simpleName = $this->routeNamingGenerator->generateSimplifiedClassName($reflection->getName());
// determine the first part of the final URL - classname or the value from PrettyRoutesController attribute
$classAttribute = $this->getControllerAttribute($reflection);
$classPath = $classAttribute?->getArguments()[PrettyRoutesController::ARGUMENT_PATH] ?? $simpleName;

return new ActionRouteDto(
Expand Down
29 changes: 29 additions & 0 deletions tests/ClassAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,33 @@ public function testDuplicatesFromDefaultsAndCustomsRemoved()
self::assertInstanceOf(ActionRouteDto::class, $routes[0]);
self::assertEquals('/specific_crud/'.$action, $routes[0]->getPath());
}

public function testControllerAttribute()
{
$controller = base64_encode(random_bytes(random_int(10, 16)));

$this->testedAnalyzer = new ClassAnalyzer(
routeNamingGenerator: new RouteNamingGenerator($this->randomPrefix),
prettyUrlsDefaultDashboard: self::DEFAULT_DASHBOARD,
prettyUrlsIncludeMenuIndex: false,
prettyUrlsDefaultActions: ['default'],
);

$this->reflectionAttribute = $this->createMock(ReflectionAttribute::class);
$this->reflectionAttribute->expects(self::any())
->method('getArguments')
->willReturn([PrettyRoutesController::ARGUMENT_DASHBOARD => $controller]);

$this->reflection->expects(self::any())
->method('getAttributes')
->with(PrettyRoutesController::class)
->willReturn([$this->reflectionAttribute]);

$routes = $this->testedAnalyzer->getRouteDtosForReflectionClass($this->reflection);

self::assertCount(1, $routes);
self::assertInstanceOf(ActionRouteDto::class, $routes[0]);
self::assertEquals('/specific_crud/default', $routes[0]->getPath());
self::assertEquals($controller, $routes[0]->getDefaults()['_controller']);
}
}

0 comments on commit 2a6780a

Please sign in to comment.