mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,32 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\CRUD\Routing;
|
||||
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Config\Loader\Loader;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
||||
use function array_filter;
|
||||
use function array_keys;
|
||||
use function array_search;
|
||||
use function in_array;
|
||||
|
||||
class CRUDRoutesLoader extends Loader
|
||||
{
|
||||
protected array $crudConfig = [];
|
||||
|
||||
protected array $apiCrudConfig = [];
|
||||
|
||||
private bool $isLoaded = false;
|
||||
private const ALL_INDEX_METHODS = [Request::METHOD_GET, Request::METHOD_HEAD];
|
||||
|
||||
private const ALL_SINGLE_METHODS = [
|
||||
Request::METHOD_GET,
|
||||
Request::METHOD_POST,
|
||||
Request::METHOD_PUT,
|
||||
Request::METHOD_DELETE
|
||||
Request::METHOD_DELETE,
|
||||
];
|
||||
|
||||
private const ALL_INDEX_METHODS = [ Request::METHOD_GET, Request::METHOD_HEAD ];
|
||||
protected array $apiCrudConfig = [];
|
||||
|
||||
protected array $crudConfig = [];
|
||||
|
||||
private bool $isLoaded = false;
|
||||
|
||||
public function __construct(array $crudConfig, array $apiCrudConfig)
|
||||
{
|
||||
@@ -37,22 +47,15 @@ class CRUDRoutesLoader extends Loader
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for CRUD and CRUD Api.
|
||||
*
|
||||
* @param mixed $resource
|
||||
* @param null $type
|
||||
* @return bool
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
return 'CRUD' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for CRUD and CRUD Api
|
||||
* @param mixed|null $type
|
||||
*/
|
||||
public function load($resource, $type = null): RouteCollection
|
||||
{
|
||||
if (true === $this->isLoaded) {
|
||||
throw new \RuntimeException('Do not add the "CRUD" loader twice');
|
||||
throw new RuntimeException('Do not add the "CRUD" loader twice');
|
||||
}
|
||||
|
||||
$collection = new RouteCollection();
|
||||
@@ -60,6 +63,7 @@ class CRUDRoutesLoader extends Loader
|
||||
foreach ($this->crudConfig as $crudConfig) {
|
||||
$collection->addCollection($this->loadCrudConfig($crudConfig));
|
||||
}
|
||||
|
||||
foreach ($this->apiCrudConfig as $crudConfig) {
|
||||
$collection->addCollection($this->loadApi($crudConfig));
|
||||
}
|
||||
@@ -68,56 +72,30 @@ class CRUDRoutesLoader extends Loader
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for CRUD (without api)
|
||||
* @param mixed $resource
|
||||
* @param null $type
|
||||
*
|
||||
* @param $crudConfig
|
||||
* @return RouteCollection
|
||||
* @return bool
|
||||
*/
|
||||
protected function loadCrudConfig($crudConfig): RouteCollection
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller ='cscrud_'.$crudConfig['name'].'_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// defaults (controller name)
|
||||
$defaults = [
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? $name)
|
||||
];
|
||||
|
||||
if ($name === 'index') {
|
||||
$path = "{_locale}".$crudConfig['base_path'];
|
||||
$route = new Route($path, $defaults);
|
||||
} elseif ($name === 'new') {
|
||||
$path = "{_locale}".$crudConfig['base_path'].'/'.$name;
|
||||
$route = new Route($path, $defaults);
|
||||
} else {
|
||||
$path = "{_locale}".$crudConfig['base_path'].($action['path'] ?? '/{id}/'.$name);
|
||||
$requirements = $action['requirements'] ?? [
|
||||
'{id}' => '\d+'
|
||||
];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
}
|
||||
|
||||
$collection->add('chill_crud_'.$crudConfig['name'].'_'.$name, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
return 'CRUD' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for api single
|
||||
* Load routes for api single.
|
||||
*
|
||||
* @param $crudConfig
|
||||
* @return RouteCollection
|
||||
*/
|
||||
protected function loadApi(array $crudConfig): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller = 'csapi_'.$crudConfig['name'].'_controller';
|
||||
$controller = 'csapi_' . $crudConfig['name'] . '_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// filter only on single actions
|
||||
$singleCollection = $action['single_collection'] ?? $name === '_entity' ? 'single' : NULL;
|
||||
$singleCollection = $action['single_collection'] ?? '_entity' === $name ? 'single' : null;
|
||||
|
||||
if ('collection' === $singleCollection) {
|
||||
// continue;
|
||||
}
|
||||
@@ -126,46 +104,61 @@ class CRUDRoutesLoader extends Loader
|
||||
switch ($name) {
|
||||
case '_entity':
|
||||
$controllerAction = 'entityApi';
|
||||
|
||||
break;
|
||||
|
||||
case '_index':
|
||||
$controllerAction = 'indexApi';
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$controllerAction = $name.'Api';
|
||||
$controllerAction = $name . 'Api';
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? $controllerAction)
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? $controllerAction),
|
||||
];
|
||||
|
||||
// path are rewritten
|
||||
// if name === 'default', we rewrite it to nothing :-)
|
||||
$localName = \in_array($name, [ '_entity', '_index' ]) ? '' : '/'.$name;
|
||||
$localName = in_array($name, ['_entity', '_index']) ? '' : '/' . $name;
|
||||
|
||||
if ('collection' === $action['single_collection'] || '_index' === $name) {
|
||||
$localPath = $action['path'] ?? $localName.'.{_format}';
|
||||
$localPath = $action['path'] ?? $localName . '.{_format}';
|
||||
} else {
|
||||
$localPath = $action['path'] ?? '/{id}'.$localName.'.{_format}';
|
||||
$localPath = $action['path'] ?? '/{id}' . $localName . '.{_format}';
|
||||
}
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
$path = $crudConfig['base_path'] . $localPath;
|
||||
|
||||
$requirements = $action['requirements'] ?? [ '{id}' => '\d+' ];
|
||||
$requirements = $action['requirements'] ?? ['{id}' => '\d+'];
|
||||
|
||||
$methods = \array_keys(\array_filter($action['methods'], function($value, $key) { return $value; },
|
||||
ARRAY_FILTER_USE_BOTH));
|
||||
$methods = array_keys(array_filter(
|
||||
$action['methods'],
|
||||
function ($value, $key) { return $value; },
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
));
|
||||
|
||||
if (count($methods) === 0) {
|
||||
throw new \RuntimeException("The api configuration named \"{$crudConfig['name']}\", action \"{$name}\", ".
|
||||
"does not have any allowed methods. You should remove this action from the config ".
|
||||
"or allow, at least, one method");
|
||||
throw new RuntimeException("The api configuration named \"{$crudConfig['name']}\", action \"{$name}\", " .
|
||||
'does not have any allowed methods. You should remove this action from the config ' .
|
||||
'or allow, at least, one method');
|
||||
}
|
||||
|
||||
if ('_entity' === $name && \in_array(Request::METHOD_POST, $methods)) {
|
||||
unset($methods[\array_search(Request::METHOD_POST, $methods)]);
|
||||
$entityPostRoute = $this->createEntityPostRoute($name, $crudConfig, $action,
|
||||
$controller);
|
||||
$collection->add("chill_api_single_{$crudConfig['name']}_{$name}_create",
|
||||
$entityPostRoute);
|
||||
if ('_entity' === $name && in_array(Request::METHOD_POST, $methods)) {
|
||||
unset($methods[array_search(Request::METHOD_POST, $methods)]);
|
||||
$entityPostRoute = $this->createEntityPostRoute(
|
||||
$name,
|
||||
$crudConfig,
|
||||
$action,
|
||||
$controller
|
||||
);
|
||||
$collection->add(
|
||||
"chill_api_single_{$crudConfig['name']}_{$name}_create",
|
||||
$entityPostRoute
|
||||
);
|
||||
}
|
||||
|
||||
if (count($methods) === 0) {
|
||||
@@ -177,7 +170,43 @@ class CRUDRoutesLoader extends Loader
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods($methods);
|
||||
|
||||
$collection->add('chill_api_single_'.$crudConfig['name'].'_'.$name, $route);
|
||||
$collection->add('chill_api_single_' . $crudConfig['name'] . '_' . $name, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load routes for CRUD (without api).
|
||||
*
|
||||
* @param $crudConfig
|
||||
*/
|
||||
protected function loadCrudConfig($crudConfig): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller = 'cscrud_' . $crudConfig['name'] . '_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// defaults (controller name)
|
||||
$defaults = [
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? $name),
|
||||
];
|
||||
|
||||
if ('index' === $name) {
|
||||
$path = '{_locale}' . $crudConfig['base_path'];
|
||||
$route = new Route($path, $defaults);
|
||||
} elseif ('new' === $name) {
|
||||
$path = '{_locale}' . $crudConfig['base_path'] . '/' . $name;
|
||||
$route = new Route($path, $defaults);
|
||||
} else {
|
||||
$path = '{_locale}' . $crudConfig['base_path'] . ($action['path'] ?? '/{id}/' . $name);
|
||||
$requirements = $action['requirements'] ?? [
|
||||
'{id}' => '\d+',
|
||||
];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
}
|
||||
|
||||
$collection->add('chill_crud_' . $crudConfig['name'] . '_' . $name, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
@@ -185,14 +214,14 @@ class CRUDRoutesLoader extends Loader
|
||||
|
||||
private function createEntityPostRoute(string $name, $crudConfig, array $action, $controller): Route
|
||||
{
|
||||
$localPath = $action['path'].'.{_format}';
|
||||
$localPath = $action['path'] . '.{_format}';
|
||||
$defaults = [
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? 'entityPost')
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? 'entityPost'),
|
||||
];
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
$path = $crudConfig['base_path'] . $localPath;
|
||||
$requirements = $action['requirements'] ?? [];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods([ Request::METHOD_POST ]);
|
||||
$route->setMethods([Request::METHOD_POST]);
|
||||
|
||||
return $route;
|
||||
}
|
||||
|
Reference in New Issue
Block a user