mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-04 05:44:58 +00:00
Initialize a CRUD for entities
This commit is contained in:
87
CRUD/Routing/CRUDRoutesLoader.php
Normal file
87
CRUD/Routing/CRUDRoutesLoader.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2019, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\CRUD\Routing;
|
||||
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class CRUDRoutesLoader
|
||||
{
|
||||
protected $config = [];
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
protected function addDummyConfig()
|
||||
{
|
||||
$this->config[] = [
|
||||
'name' => 'country',
|
||||
'actions' => ['index'],//, 'new', 'edit', 'delete'],
|
||||
'base_path' => '/admin/country',
|
||||
'controller' => \Chill\MainBundle\Controller\AdminCountryCRUDController::class
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function load()
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
|
||||
foreach ($this->config as $config) {
|
||||
$collection->addCollection($this->loadConfig($config));
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
protected function loadConfig($config): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
|
||||
foreach ($config['actions'] as $action) {
|
||||
$defaults = [
|
||||
'_controller' => $config['controller'].'::'.$action
|
||||
];
|
||||
|
||||
if ($action === 'index') {
|
||||
$path = "{_locale}".$config['base_path'];
|
||||
$route = new Route($path, $defaults);
|
||||
} else {
|
||||
$path = "{_locale}".$config['base_path'].'/{id}/'.$action;
|
||||
$requirements = [
|
||||
'{id}' => '\d+'
|
||||
];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
}
|
||||
|
||||
$collection->add('chill_crud_'.$config['name'].'_'.$action, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user