first impl for index action

This commit is contained in:
2021-05-06 12:08:45 +02:00
parent 07e0692783
commit 2b8bbe019d
10 changed files with 375 additions and 60 deletions

View File

@@ -90,8 +90,7 @@ class CRUDRoutesLoader extends Loader
$collection->addCollection($this->loadCrudConfig($crudConfig));
}
foreach ($this->apiConfig as $crudConfig) {
$collection->addCollection($this->loadApiSingle($crudConfig));
//$collection->addCollection($this->loadApiMulti($crudConfig));
$collection->addCollection($this->loadApi($crudConfig));
}
return $collection;
@@ -140,7 +139,7 @@ class CRUDRoutesLoader extends Loader
* @param $crudConfig
* @return RouteCollection
*/
protected function loadApiSingle(array $crudConfig): RouteCollection
protected function loadApi(array $crudConfig): RouteCollection
{
$collection = new RouteCollection();
$controller ='csapi_'.$crudConfig['name'].'_controller';
@@ -149,6 +148,65 @@ class CRUDRoutesLoader extends Loader
// filter only on single actions
$singleCollection = $action['single-collection'] ?? $name === '_entity' ? 'single' : NULL;
if ('collection' === $singleCollection) {
// continue;
}
// compute default action
switch ($name) {
case '_entity':
$controllerAction = 'entityApi';
break;
case '_index':
$controllerAction = 'indexApi';
break;
default:
$controllerAction = $name.'Api';
break;
}
$defaults = [
'_controller' => $controller.':'.($action['controller_action'] ?? $controllerAction)
];
// path are rewritten
// if name === 'default', we rewrite it to nothing :-)
$localName = \in_array($name, [ '_entity', '_index' ]) ? '' : '/'.$name;
if ('collection' === $action['single-collection'] || '_index' === $name) {
$localPath = $action['path'] ?? $localName.'.{_format}';
} else {
$localPath = $action['path'] ?? '/{id}'.$localName.'.{_format}';
}
$path = $crudConfig['base_path'].$localPath;
$requirements = $action['requirements'] ?? [ '{id}' => '\d+' ];
$methods = \array_keys(\array_filter($action['methods'], function($value, $key) { return $value; },
ARRAY_FILTER_USE_BOTH));
$route = new Route($path, $defaults, $requirements);
$route->setMethods($methods);
$collection->add('chill_api_single_'.$crudConfig['name'].'_'.$name, $route);
}
return $collection;
}
/**
* Load routes for api multi
*
* @param $crudConfig
* @return RouteCollection
*/
protected function loadApiMultiConfig(array $crudConfig): RouteCollection
{
$collection = new RouteCollection();
$controller ='csapi_'.$crudConfig['name'].'_controller';
foreach ($crudConfig['actions'] as $name => $action) {
// filter only on single actions
$singleCollection = $action['single-collection'] ?? $name === '_index' ? 'collection' : NULL;
if ('single' === $singleCollection) {
continue;
}
@@ -175,33 +233,4 @@ class CRUDRoutesLoader extends Loader
return $collection;
}
/**
* Load routes for api multi
*
* @param $crudConfig
* @return RouteCollection
*/
protected function loadApiMultiConfig(array $crudConfig): RouteCollection
{
$collection = new RouteCollection();
foreach ($crudConfig['actions_multi'] as $name => $action) {
// we compute the data from configuration to a local form
$defaults = [
'_controller' => 'cscrud_'.$crudConfig['name'].'_controller'.':'.($action['controller_action'] ?? $name.'Api')
];
// path are rewritten
// if name === 'index', we rewrite it to nothing :-)
$localName = 'index' === $name ? '' : $name;
$localPath = $action['path'] ?? '.{_format}';
$path = $crudConfig['base_path'].$localPath.$name;
$requirements = $action['requirements'] ?? [ '{id}' => '\d+' ];
$methods = $name === 'default' ? self::ALL_MULTI_METHODS: [];
$route = new Route($path, $defaults, $requirements);
$collection->add('chill_api_multi'.$crudConfig['name'].'_'.$name, $route);
}
return $collection;
}
}