Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • AnnotationClassLoader
  • AnnotationDirectoryLoader
  • AnnotationFileLoader
  • ClosureLoader
  • DirectoryLoader
  • ObjectRouteLoader
  • PhpFileLoader
  • RecursiveCallbackFilterIterator
  • XmlFileLoader
  • YamlFileLoader
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the Symfony package.
  5:  *
  6:  * (c) Fabien Potencier <[email protected]>
  7:  *
  8:  * For the full copyright and license information, please view the LICENSE
  9:  * file that was distributed with this source code.
 10:  */
 11: 
 12: namespace Symfony\Component\Routing\Loader;
 13: 
 14: use Symfony\Component\Routing\RouteCollection;
 15: use Symfony\Component\Routing\Route;
 16: use Symfony\Component\Config\Resource\FileResource;
 17: use Symfony\Component\Yaml\Exception\ParseException;
 18: use Symfony\Component\Yaml\Parser as YamlParser;
 19: use Symfony\Component\Config\Loader\FileLoader;
 20: 
 21: /**
 22:  * YamlFileLoader loads Yaml routing files.
 23:  *
 24:  * @author Fabien Potencier <[email protected]>
 25:  * @author Tobias Schultze <http://tobion.de>
 26:  */
 27: class YamlFileLoader extends FileLoader
 28: {
 29:     private static $availableKeys = array(
 30:         'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
 31:     );
 32:     private $yamlParser;
 33: 
 34:     /**
 35:      * Loads a Yaml file.
 36:      *
 37:      * @param string      $file A Yaml file path
 38:      * @param string|null $type The resource type
 39:      *
 40:      * @return RouteCollection A RouteCollection instance
 41:      *
 42:      * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
 43:      */
 44:     public function load($file, $type = null)
 45:     {
 46:         $path = $this->locator->locate($file);
 47: 
 48:         if (!stream_is_local($path)) {
 49:             throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
 50:         }
 51: 
 52:         if (!file_exists($path)) {
 53:             throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
 54:         }
 55: 
 56:         if (null === $this->yamlParser) {
 57:             $this->yamlParser = new YamlParser();
 58:         }
 59: 
 60:         try {
 61:             $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
 62:         } catch (ParseException $e) {
 63:             throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
 64:         }
 65: 
 66:         $collection = new RouteCollection();
 67:         $collection->addResource(new FileResource($path));
 68: 
 69:         // empty file
 70:         if (null === $parsedConfig) {
 71:             return $collection;
 72:         }
 73: 
 74:         // not an array
 75:         if (!is_array($parsedConfig)) {
 76:             throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
 77:         }
 78: 
 79:         foreach ($parsedConfig as $name => $config) {
 80:             if (isset($config['pattern'])) {
 81:                 if (isset($config['path'])) {
 82:                     throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
 83:                 }
 84: 
 85:                 @trigger_error(sprintf('The "pattern" option in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', $path), E_USER_DEPRECATED);
 86: 
 87:                 $config['path'] = $config['pattern'];
 88:                 unset($config['pattern']);
 89:             }
 90: 
 91:             $this->validate($config, $name, $path);
 92: 
 93:             if (isset($config['resource'])) {
 94:                 $this->parseImport($collection, $config, $path, $file);
 95:             } else {
 96:                 $this->parseRoute($collection, $name, $config, $path);
 97:             }
 98:         }
 99: 
100:         return $collection;
101:     }
102: 
103:     /**
104:      * {@inheritdoc}
105:      */
106:     public function supports($resource, $type = null)
107:     {
108:         return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
109:     }
110: 
111:     /**
112:      * Parses a route and adds it to the RouteCollection.
113:      *
114:      * @param RouteCollection $collection A RouteCollection instance
115:      * @param string          $name       Route name
116:      * @param array           $config     Route definition
117:      * @param string          $path       Full path of the YAML file being processed
118:      */
119:     protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
120:     {
121:         $defaults = isset($config['defaults']) ? $config['defaults'] : array();
122:         $requirements = isset($config['requirements']) ? $config['requirements'] : array();
123:         $options = isset($config['options']) ? $config['options'] : array();
124:         $host = isset($config['host']) ? $config['host'] : '';
125:         $schemes = isset($config['schemes']) ? $config['schemes'] : array();
126:         $methods = isset($config['methods']) ? $config['methods'] : array();
127:         $condition = isset($config['condition']) ? $config['condition'] : null;
128: 
129:         if (isset($requirements['_method'])) {
130:             if (0 === count($methods)) {
131:                 $methods = explode('|', $requirements['_method']);
132:             }
133: 
134:             unset($requirements['_method']);
135:             @trigger_error(sprintf('The "_method" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "methods" option instead.', $name, $path), E_USER_DEPRECATED);
136:         }
137: 
138:         if (isset($requirements['_scheme'])) {
139:             if (0 === count($schemes)) {
140:                 $schemes = explode('|', $requirements['_scheme']);
141:             }
142: 
143:             unset($requirements['_scheme']);
144:             @trigger_error(sprintf('The "_scheme" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "schemes" option instead.', $name, $path), E_USER_DEPRECATED);
145:         }
146: 
147:         $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
148: 
149:         $collection->add($name, $route);
150:     }
151: 
152:     /**
153:      * Parses an import and adds the routes in the resource to the RouteCollection.
154:      *
155:      * @param RouteCollection $collection A RouteCollection instance
156:      * @param array           $config     Route definition
157:      * @param string          $path       Full path of the YAML file being processed
158:      * @param string          $file       Loaded file name
159:      */
160:     protected function parseImport(RouteCollection $collection, array $config, $path, $file)
161:     {
162:         $type = isset($config['type']) ? $config['type'] : null;
163:         $prefix = isset($config['prefix']) ? $config['prefix'] : '';
164:         $defaults = isset($config['defaults']) ? $config['defaults'] : array();
165:         $requirements = isset($config['requirements']) ? $config['requirements'] : array();
166:         $options = isset($config['options']) ? $config['options'] : array();
167:         $host = isset($config['host']) ? $config['host'] : null;
168:         $condition = isset($config['condition']) ? $config['condition'] : null;
169:         $schemes = isset($config['schemes']) ? $config['schemes'] : null;
170:         $methods = isset($config['methods']) ? $config['methods'] : null;
171: 
172:         $this->setCurrentDir(dirname($path));
173: 
174:         $subCollection = $this->import($config['resource'], $type, false, $file);
175:         /* @var $subCollection RouteCollection */
176:         $subCollection->addPrefix($prefix);
177:         if (null !== $host) {
178:             $subCollection->setHost($host);
179:         }
180:         if (null !== $condition) {
181:             $subCollection->setCondition($condition);
182:         }
183:         if (null !== $schemes) {
184:             $subCollection->setSchemes($schemes);
185:         }
186:         if (null !== $methods) {
187:             $subCollection->setMethods($methods);
188:         }
189:         $subCollection->addDefaults($defaults);
190:         $subCollection->addRequirements($requirements);
191:         $subCollection->addOptions($options);
192: 
193:         $collection->addCollection($subCollection);
194:     }
195: 
196:     /**
197:      * Validates the route configuration.
198:      *
199:      * @param array  $config A resource config
200:      * @param string $name   The config key
201:      * @param string $path   The loaded file path
202:      *
203:      * @throws \InvalidArgumentException If one of the provided config keys is not supported,
204:      *                                   something is missing or the combination is nonsense
205:      */
206:     protected function validate($config, $name, $path)
207:     {
208:         if (!is_array($config)) {
209:             throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
210:         }
211:         if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
212:             throw new \InvalidArgumentException(sprintf(
213:                 'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
214:                 $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
215:             ));
216:         }
217:         if (isset($config['resource']) && isset($config['path'])) {
218:             throw new \InvalidArgumentException(sprintf(
219:                 'The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.',
220:                 $path, $name
221:             ));
222:         }
223:         if (!isset($config['resource']) && isset($config['type'])) {
224:             throw new \InvalidArgumentException(sprintf(
225:                 'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
226:                 $name, $path
227:             ));
228:         }
229:         if (!isset($config['resource']) && !isset($config['path'])) {
230:             throw new \InvalidArgumentException(sprintf(
231:                 'You must define a "path" for the route "%s" in file "%s".',
232:                 $name, $path
233:             ));
234:         }
235:     }
236: }
237: 
Ratchet API documentation generated by ApiGen 2.8.0