123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace Sonata\AdminBundle\Route;
- use Symfony\Component\Routing\Route;
- class RouteCollection
- {
-
- protected $elements = array();
-
- protected $baseCodeRoute;
-
- protected $baseRouteName;
-
- protected $baseControllerName;
-
- protected $baseRoutePattern;
-
- public function __construct($baseCodeRoute, $baseRouteName, $baseRoutePattern, $baseControllerName)
- {
- $this->baseCodeRoute = $baseCodeRoute;
- $this->baseRouteName = $baseRouteName;
- $this->baseRoutePattern = $baseRoutePattern;
- $this->baseControllerName = $baseControllerName;
- }
-
- public function add($name, $pattern = null, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', array $schemes = array(), array $methods = array(), $condition = '')
- {
- $pattern = $this->baseRoutePattern.'/'.($pattern ?: $name);
- $code = $this->getCode($name);
- $routeName = $this->baseRouteName.'_'.$name;
- if (!isset($defaults['_controller'])) {
- $defaults['_controller'] = $this->baseControllerName.':'.$this->actionify($code);
- }
- if (!isset($defaults['_sonata_admin'])) {
- $defaults['_sonata_admin'] = $this->baseCodeRoute;
- }
- $defaults['_sonata_name'] = $routeName;
- $this->elements[$this->getCode($name)] = function () use (
- $pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition) {
- return new Route($pattern, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
- };
- return $this;
- }
-
- public function getCode($name)
- {
- if (strrpos($name, '.') !== false) {
- return $name;
- }
- return $this->baseCodeRoute.'.'.$name;
- }
-
- public function addCollection(RouteCollection $collection)
- {
- foreach ($collection->getElements() as $code => $route) {
- $this->elements[$code] = $route;
- }
- return $this;
- }
-
- public function getElements()
- {
- foreach ($this->elements as $name => $element) {
- $this->elements[$name] = $this->resolve($element);
- }
- return $this->elements;
- }
-
- public function has($name)
- {
- return array_key_exists($this->getCode($name), $this->elements);
- }
-
- public function get($name)
- {
- if ($this->has($name)) {
- $code = $this->getCode($name);
- $this->elements[$code] = $this->resolve($this->elements[$code]);
- return $this->elements[$code];
- }
- throw new \InvalidArgumentException(sprintf('Element "%s" does not exist.', $name));
- }
-
- public function remove($name)
- {
- unset($this->elements[$this->getCode($name)]);
- return $this;
- }
-
- public function clearExcept($routeList)
- {
- if (!is_array($routeList)) {
- $routeList = array($routeList);
- }
- $routeCodeList = array();
- foreach ($routeList as $name) {
- $routeCodeList[] = $this->getCode($name);
- }
- $elements = $this->elements;
- foreach ($elements as $key => $element) {
- if (!in_array($key, $routeCodeList)) {
- unset($this->elements[$key]);
- }
- }
- return $this;
- }
-
- public function clear()
- {
- $this->elements = array();
- return $this;
- }
-
- public function actionify($action)
- {
- if (($pos = strrpos($action, '.')) !== false) {
- $action = substr($action, $pos + 1);
- }
-
-
- if (strpos($this->baseControllerName, ':') === false) {
- $action .= 'Action';
- }
- return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
- }
-
- public function getBaseCodeRoute()
- {
- return $this->baseCodeRoute;
- }
-
- public function getBaseControllerName()
- {
- return $this->baseControllerName;
- }
-
- public function getBaseRouteName()
- {
- return $this->baseRouteName;
- }
-
- public function getBaseRoutePattern()
- {
- return $this->baseRoutePattern;
- }
-
- private function resolve($element)
- {
- if (is_callable($element)) {
- return call_user_func($element);
- }
- return $element;
- }
- }
|