* * 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 . */ namespace Chill\MainBundle\CRUD\Routing; use Symfony\Component\Config\Loader\Loader; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; /** * Class CRUDRoutesLoader * Load the route for CRUD * * @package Chill\MainBundle\CRUD\Routing */ class CRUDRoutesLoader extends Loader { /** * @var array */ protected $config = []; /** * @var bool */ private $isLoaded = false; /** * CRUDRoutesLoader constructor. * * @param $config */ public function __construct($config) { $this->config = $config; } /** * @param mixed $resource * @param null $type * @return bool */ public function supports($resource, $type = null) { return 'CRUD' === $type; } /** * @return RouteCollection */ public function load($resource, $type = null) { if (true === $this->isLoaded) { throw new \RuntimeException('Do not add the "CRUD" loader twice'); } $collection = new RouteCollection(); foreach ($this->config as $config) { $collection->addCollection($this->loadConfig($config)); } return $collection; } /** * @param $config * @return RouteCollection */ protected function loadConfig($config): RouteCollection { $collection = new RouteCollection(); foreach ($config['actions'] as $name => $action) { $defaults = [ '_controller' => 'cscrud_'.$config['name'].'_controller'.':'.($action['controller_action'] ?? $name) ]; if ($name === 'index') { $path = "{_locale}".$config['base_path']; $route = new Route($path, $defaults); } elseif ($name === 'new') { $path = "{_locale}".$config['base_path'].'/'.$name; $route = new Route($path, $defaults); } else { $path = "{_locale}".$config['base_path'].($action['path'] ?? '/{id}/'.$name); $requirements = $action['requirements'] ?? [ '{id}' => '\d+' ]; $route = new Route($path, $defaults, $requirements); } $collection->add('chill_crud_'.$config['name'].'_'.$name, $route); } return $collection; } }