Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make the bundle work with 2.0 and 2.1 at once

  • Loading branch information...
commit d58735b3f90745db46afb1fc6aba838bdda1138c 1 parent 1e008b6
@lsmith77 lsmith77 authored
View
9 DependencyInjection/FOSRestExtension.php
@@ -16,10 +16,13 @@
Symfony\Component\DependencyInjection\Reference,
Symfony\Component\DependencyInjection\ContainerInterface,
Symfony\Component\DependencyInjection\ContainerBuilder,
- Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+ Symfony\Component\DependencyInjection\Loader\XmlFileLoader,
+ Symfony\Component\HttpKernel\Kernel;
use FOS\Rest\Util\Codes;
+use FOS\RestBundle\FOSRestBundle;
+
class FOSRestExtension extends Extension
{
/**
@@ -37,6 +40,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('routing.xml');
$loader->load('util.xml');
+ if (version_compare(FOSRestBundle::getSymfonyVersion(Kernel::VERSION), '2.1.0', '<')) {
+ $container->setParameter('fos_rest.routing.loader.controller.class', $container->getParameter('fos_rest.routing.loader_2_0.controller.class'));
+ }
+
$formats = array();
foreach ($config['view']['formats'] as $format => $enabled) {
if ($enabled) {
View
15 FOSRestBundle.php
@@ -26,4 +26,19 @@ public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ConfigurationCheckPass());
}
+
+ /**
+ * Returns a cleaned version number
+ *
+ * @static
+ * @param $version
+ * @return string
+ */
+ public static function getSymfonyVersion($version)
+ {
+ return implode('.', array_slice(array_map(function($val)
+ {
+ return (int)$val;
+ }, explode('.', $version)), 0, 3));
+ }
}
View
5 README.md
@@ -1,9 +1,6 @@
FOSRestBundle
=============
-> Note: This branch is compatible with Symfony2 master (2.1.x).
-> For Symfony2 2.0.x compatibility use the 0.6 branch.
-
This bundle provides various tools to rapidly develop RESTful API's &
applications with Symfony2. Features include:
@@ -22,8 +19,6 @@ file in this bundle:
[Read the Documentation for master](https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/index.md)
-[Read the Documentation for 0.6](https://github.com/FriendsOfSymfony/FOSRestBundle/blob/0.6/Resources/doc/index.md)
-
Installation
------------
View
1  Resources/config/routing.xml
@@ -6,6 +6,7 @@
<parameters>
+ <parameter key="fos_rest.routing.loader_2_0.controller.class">FOS\RestBundle\Routing\Loader\RestRouteLoader20</parameter>
<parameter key="fos_rest.routing.loader.controller.class">FOS\RestBundle\Routing\Loader\RestRouteLoader</parameter>
<parameter key="fos_rest.routing.loader.yaml_collection.class">FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader</parameter>
<parameter key="fos_rest.routing.loader.xml_collection.class">FOS\RestBundle\Routing\Loader\RestXmlCollectionLoader</parameter>
View
149 Routing/Loader/AbstractRestRouteLoader.php
@@ -0,0 +1,149 @@
+<?php
+
+/*
+ * This file is part of the FOSRestBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace FOS\RestBundle\Routing\Loader;
+
+use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser,
+ Symfony\Component\DependencyInjection\ContainerInterface,
+ Symfony\Component\Config\Loader\LoaderInterface,
+ Symfony\Component\HttpFoundation\Request;
+
+use FOS\RestBundle\Routing\Loader\Reader\RestControllerReader;
+
+/**
+ * RestRouteLoader REST-enabled controller router loader.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
+ */
+class AbstractRestRouteLoader
+{
+ protected $container;
+ protected $controllerParser;
+ protected $controllerReader;
+ protected $defaultFormat;
+
+ /**
+ * Initializes loader.
+ *
+ * @param ContainerInterface $container service container
+ * @param ControllerNameParser $controllerParser controller name parser
+ * @param RestControllerReader $controllerReader controller reader
+ * @param string $defaultFormat default http format
+ */
+ public function __construct(ContainerInterface $container,
+ ControllerNameParser $controllerParser,
+ RestControllerReader $controllerReader, $defaultFormat = 'html')
+ {
+ $this->container = $container;
+ $this->controllerParser = $controllerParser;
+ $this->controllerReader = $controllerReader;
+ $this->defaultFormat = $defaultFormat;
+ }
+
+ /**
+ * Returns controller reader.
+ *
+ * @return RestControllerReader
+ */
+ public function getControllerReader()
+ {
+ return $this->controllerReader;
+ }
+
+ /**
+ * Loads a Routes collection by parsing Controller method names.
+ *
+ * @param string $controller Some identifier for the controller
+ * @param string $type The resource type
+ *
+ * @return RouteCollection A RouteCollection instance
+ */
+ public function load($controller, $type = null)
+ {
+ list($prefix, $class) = $this->getControllerLocator($controller);
+
+ $collection = $this->controllerReader->read(new \ReflectionClass($class));
+ $collection->prependRouteControllersWithPrefix($prefix);
+ $collection->setDefaultFormat($this->defaultFormat);
+
+ return $collection;
+ }
+
+ /**
+ * Returns true if this class supports the given resource.
+ *
+ * @param mixed $resource A resource
+ * @param string $type The resource type
+ *
+ * @return Boolean true if this class supports the given resource, false otherwise
+ */
+ public function supports($resource, $type = null)
+ {
+ return is_string($resource)
+ && 'rest' === $type
+ && !in_array(pathinfo($resource, PATHINFO_EXTENSION), array('xml', 'yml'));
+ }
+
+ /**
+ * Gets the loader resolver.
+ *
+ * @return LoaderResolverInterface A LoaderResolver instance
+ */
+ public function getResolver()
+ {
+ }
+
+ /**
+ * Returns controller locator by it's id.
+ *
+ * @param string $controller
+ *
+ * @return array
+ */
+ private function getControllerLocator($controller)
+ {
+ $class = null;
+ $prefix = null;
+
+ if (class_exists($controller)) {
+ // full class name
+ $class = $controller;
+ $prefix = $class . '::';
+ } elseif (false !== strpos($controller, ':')) {
+ // bundle:controller notation
+ try {
+ $notation = $this->controllerParser->parse($controller . ':method');
+ list($class, $method) = explode('::', $notation);
+ $prefix = $class . '::';
+ } catch (\Exception $e) {
+ throw new \InvalidArgumentException(
+ sprintf('Can\'t locate "%s" controller.', $controller)
+ );
+ }
+ } elseif ($this->container->has($controller)) {
+ // service_id
+ $prefix = $controller . ':';
+ $this->container->enterScope('request');
+ $this->container->set('request', new Request);
+ $class = get_class($this->container->get($controller));
+ $this->container->leaveScope('request');
+ }
+
+ if (empty($class)) {
+ throw new \InvalidArgumentException(sprintf(
+ 'Class could not be determined for Controller identified by "%s".', $controller
+ ));
+ }
+
+ return array($prefix, $class);
+ }
+}
View
132 Routing/Loader/RestRouteLoader.php
@@ -11,13 +11,8 @@
namespace FOS\RestBundle\Routing\Loader;
-use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser,
- Symfony\Component\DependencyInjection\ContainerInterface,
- Symfony\Component\Config\Loader\LoaderInterface,
- Symfony\Component\Config\Loader\LoaderResolverInterface,
- Symfony\Component\HttpFoundation\Request;
-
-use FOS\RestBundle\Routing\Loader\Reader\RestControllerReader;
+use Symfony\Component\Config\Loader\LoaderInterface,
+ Symfony\Component\Config\Loader\LoaderResolverInterface;
/**
* RestRouteLoader REST-enabled controller router loader.
@@ -25,84 +20,8 @@
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
-class RestRouteLoader implements LoaderInterface
+class RestRouteLoader extends AbstractRestRouteLoader implements LoaderInterface
{
- private $container;
- private $controllerParser;
- private $controllerReader;
- private $defaultFormat;
-
- /**
- * Initializes loader.
- *
- * @param ContainerInterface $container service container
- * @param ControllerNameParser $controllerParser controller name parser
- * @param RestControllerReader $controllerReader controller reader
- * @param string $defaultFormat default http format
- */
- public function __construct(ContainerInterface $container,
- ControllerNameParser $controllerParser,
- RestControllerReader $controllerReader, $defaultFormat = 'html')
- {
- $this->container = $container;
- $this->controllerParser = $controllerParser;
- $this->controllerReader = $controllerReader;
- $this->defaultFormat = $defaultFormat;
- }
-
- /**
- * Returns controller reader.
- *
- * @return RestControllerReader
- */
- public function getControllerReader()
- {
- return $this->controllerReader;
- }
-
- /**
- * Loads a Routes collection by parsing Controller method names.
- *
- * @param string $controller Some identifier for the controller
- * @param string $type The resource type
- *
- * @return RouteCollection A RouteCollection instance
- */
- public function load($controller, $type = null)
- {
- list($prefix, $class) = $this->getControllerLocator($controller);
-
- $collection = $this->controllerReader->read(new \ReflectionClass($class));
- $collection->prependRouteControllersWithPrefix($prefix);
- $collection->setDefaultFormat($this->defaultFormat);
-
- return $collection;
- }
-
- /**
- * Returns true if this class supports the given resource.
- *
- * @param mixed $resource A resource
- * @param string $type The resource type
- *
- * @return Boolean true if this class supports the given resource, false otherwise
- */
- public function supports($resource, $type = null)
- {
- return is_string($resource)
- && 'rest' === $type
- && !in_array(pathinfo($resource, PATHINFO_EXTENSION), array('xml', 'yml'));
- }
-
- /**
- * Gets the loader resolver.
- *
- * @return LoaderResolverInterface A LoaderResolver instance
- */
- public function getResolver()
- {
- }
-
/**
* Sets the loader resolver.
*
@@ -111,49 +30,4 @@ public function getResolver()
public function setResolver(LoaderResolverInterface $resolver)
{
}
-
- /**
- * Returns controller locator by it's id.
- *
- * @param string $controller
- *
- * @return array
- */
- private function getControllerLocator($controller)
- {
- $class = null;
- $prefix = null;
-
- if (class_exists($controller)) {
- // full class name
- $class = $controller;
- $prefix = $class . '::';
- } elseif (false !== strpos($controller, ':')) {
- // bundle:controller notation
- try {
- $notation = $this->controllerParser->parse($controller . ':method');
- list($class, $method) = explode('::', $notation);
- $prefix = $class . '::';
- } catch (\Exception $e) {
- throw new \InvalidArgumentException(
- sprintf('Can\'t locate "%s" controller.', $controller)
- );
- }
- } elseif ($this->container->has($controller)) {
- // service_id
- $prefix = $controller . ':';
- $this->container->enterScope('request');
- $this->container->set('request', new Request);
- $class = get_class($this->container->get($controller));
- $this->container->leaveScope('request');
- }
-
- if (empty($class)) {
- throw new \InvalidArgumentException(sprintf(
- 'Class could not be determined for Controller identified by "%s".', $controller
- ));
- }
-
- return array($prefix, $class);
- }
}
View
35 Routing/Loader/RestRouteLoader20.php
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the FOSRestBundle package.
+ *
+ * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace FOS\RestBundle\Routing\Loader;
+
+use Symfony\Component\Config\Loader\LoaderInterface,
+ Symfony\Component\Config\Loader\LoaderResolver;
+
+/**
+ * RestRouteLoader REST-enabled controller router loader.
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
+ *
+ * @codeCoverageIgnore
+ */
+class RestRouteLoader20 extends AbstractRestRouteLoader implements LoaderInterface
+{
+ /**
+ * Sets the loader resolver.
+ *
+ * @param LoaderResolver $resolver A LoaderResolver instance
+ */
+ public function setResolver(LoaderResolver $resolver)
+ {
+ }
+}
View
3  Tests/Routing/Loader/RestRouteLoaderTest.php
@@ -11,8 +11,7 @@
namespace FOS\RestBundle\Tests\Routing\Loader;
-use FOS\RestBundle\Routing\Loader\RestRouteLoader,
- FOS\RestBundle\Routing\RestRouteCollection;
+use FOS\RestBundle\Routing\RestRouteCollection;
/**
* RestRouteLoader test.
View
3  Tests/Routing/Loader/RestXmlCollectionLoaderTest.php
@@ -14,8 +14,7 @@
use Symfony\Component\Config\Loader\LoaderResolver,
Symfony\Component\Config\FileLocator;
-use FOS\RestBundle\Routing\Loader\RestRouteLoader,
- FOS\RestBundle\Routing\Loader\RestRouteProcessor,
+use FOS\RestBundle\Routing\Loader\RestRouteProcessor,
FOS\RestBundle\Routing\Loader\RestXmlCollectionLoader;
/**
View
3  Tests/Routing/Loader/RestYamlCollectionLoaderTest.php
@@ -14,8 +14,7 @@
use Symfony\Component\Config\Loader\LoaderResolver,
Symfony\Component\Config\FileLocator;
-use FOS\RestBundle\Routing\Loader\RestRouteLoader,
- FOS\RestBundle\Routing\Loader\RestRouteProcessor,
+use FOS\RestBundle\Routing\Loader\RestRouteProcessor,
FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader;
/**
View
2  composer.json
@@ -23,7 +23,7 @@
"require": {
"php": ">=5.3.2",
"friendsofsymfony/rest": "*",
- "symfony/symfony": ">=2.1",
+ "symfony/symfony": ">=2.0,<2.2",
"sensio/framework-extra-bundle": "*",
"jms/serializer-bundle": "*"
},
View
1  phpunit.xml.dist
@@ -12,6 +12,7 @@
<whitelist>
<directory>./</directory>
<exclude>
+ <file>./Routing/Loader/RestRouteLoader20.php</file>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
Please sign in to comment.
Something went wrong with that request. Please try again.