diff --git a/Controller/ExceptionController.php b/Controller/ExceptionController.php index 1bfa06f80..6363c74d0 100644 --- a/Controller/ExceptionController.php +++ b/Controller/ExceptionController.php @@ -20,7 +20,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use FOS\RestBundle\View\ViewHandler; use FOS\RestBundle\View\View; use FOS\RestBundle\Util\ExceptionWrapper; diff --git a/Controller/FOSRestController.php b/Controller/FOSRestController.php index 832226af3..21147f6a1 100644 --- a/Controller/FOSRestController.php +++ b/Controller/FOSRestController.php @@ -17,7 +17,7 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\RedirectView; use FOS\RestBundle\View\RouteRedirectView; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * Base Controller for Controllers using the View functionality of FOSRestBundle. diff --git a/Decoder/ContainerDecoderProvider.php b/Decoder/ContainerDecoderProvider.php index 43c3f57c9..72aedb6f5 100644 --- a/Decoder/ContainerDecoderProvider.php +++ b/Decoder/ContainerDecoderProvider.php @@ -13,7 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerAware; -use FOS\Rest\Decoder\DecoderProviderInterface; +use FOS\RestBundle\Decoder\DecoderProviderInterface; /** * Provides encoders through the Symfony2 DIC @@ -51,7 +51,7 @@ public function supports($format) * @param string $format format * * @throws \InvalidArgumentException - * @return FOS\Rest\Decoder\DecoderInterface + * @return FOS\RestBundle\Decoder\DecoderInterface */ public function getDecoder($format) { diff --git a/Decoder/DecoderInterface.php b/Decoder/DecoderInterface.php new file mode 100644 index 000000000..1222d7d7d --- /dev/null +++ b/Decoder/DecoderInterface.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Decoder; + +/** + * Defines the interface of decoders + * + * @author Jordi Boggiano + */ +interface DecoderInterface +{ + /** + * Decodes a string into PHP data + * + * @param string $data data to decode + * @return array|Boolean false in case the content could not be decoded, else an array + */ + function decode($data); +} diff --git a/Decoder/DecoderProviderInterface.php b/Decoder/DecoderProviderInterface.php new file mode 100644 index 000000000..ae1f9f5c3 --- /dev/null +++ b/Decoder/DecoderProviderInterface.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Decoder; + +/** + * Defines the interface of decoder providers + * + * @author Igor Wiedler + */ +interface DecoderProviderInterface +{ + /** + * Check if a certain format is supported. + * + * @param string $format Format for the requested decoder. + * @return Boolean + */ + function supports($format); + + /** + * Provides decoders, possibly lazily. + * + * @param string $format Format for the requested decoder. + * @return FOS\RestBundle\Decoder\DecoderInterface + */ + function getDecoder($format); +} diff --git a/Decoder/JsonDecoder.php b/Decoder/JsonDecoder.php new file mode 100644 index 000000000..5dafdb1d8 --- /dev/null +++ b/Decoder/JsonDecoder.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Decoder; + +/** + * Decodes JSON data + * + * @author Jordi Boggiano + */ +class JsonDecoder implements DecoderInterface +{ + /** + * {@inheritdoc} + */ + public function decode($data) + { + return @json_decode($data, true); + } +} diff --git a/Decoder/JsonToFormDecoder.php b/Decoder/JsonToFormDecoder.php new file mode 100644 index 000000000..3fd399458 --- /dev/null +++ b/Decoder/JsonToFormDecoder.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Decoder; + +use FOS\RestBundle\Decoder\DecoderInterface; + +/** + * Decodes JSON data and make it compliant with application/x-www-form-encoded style + * + * @author Kévin Dunglas + */ +class JsonToFormDecoder implements DecoderInterface +{ + + /** + * Makes data decoded from JSON application/x-www-form-encoded compliant + * + * @param array $data + */ + private function xWwwFormEncodedLike(&$data) + { + foreach ($data as $key => &$value) { + if (is_array($value)) { + // Encode recursively + $this->xWwwFormEncodedLike($value); + } elseif (false === $value) { + // Checkbox-like behavior: remove false data + unset($data[$key]); + } elseif (!is_string($value)) { + // Convert everyting to string + // true values will be converted to '1', this is the default checkbox behavior + $value = strval($value); + } + } + } + + /** + * {@inheritdoc} + */ + public function decode($data) + { + $decodedData = @json_decode($data, true); + if ($decodedData) { + $this->xWwwFormEncodedLike($decodedData); + } + + return $decodedData; + } + +} \ No newline at end of file diff --git a/Decoder/XmlDecoder.php b/Decoder/XmlDecoder.php new file mode 100644 index 000000000..643289329 --- /dev/null +++ b/Decoder/XmlDecoder.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Decoder; + +use Symfony\Component\Serializer\Encoder\XmlEncoder; + +/** + * Decodes XML data + * + * @author Jordi Boggiano + * @author John Wards + * @author Fabian Vogler + */ +class XmlDecoder implements DecoderInterface +{ + private $encoder; + + public function __construct() + { + $this->encoder = new XmlEncoder(); + } + + /** + * {@inheritdoc} + */ + public function decode($data) + { + return $this->encoder->decode($data, 'xml'); + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index b940e95ae..a61e814f3 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -15,7 +15,7 @@ use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\ConfigurationInterface; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * This class contains the configuration information for the bundle diff --git a/DependencyInjection/FOSRestExtension.php b/DependencyInjection/FOSRestExtension.php index b1a9712d8..58f1cdeca 100644 --- a/DependencyInjection/FOSRestExtension.php +++ b/DependencyInjection/FOSRestExtension.php @@ -20,7 +20,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\Kernel; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use FOS\RestBundle\FOSRestBundle; @@ -83,12 +83,12 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter($this->getAlias().'.force_redirects', $config['view']['force_redirects']); if (!is_numeric($config['view']['failed_validation'])) { - $config['view']['failed_validation'] = constant('\FOS\Rest\Util\Codes::'.$config['view']['failed_validation']); + $config['view']['failed_validation'] = constant('\FOS\RestBundle\Util\Codes::'.$config['view']['failed_validation']); } $container->setParameter($this->getAlias().'.failed_validation', $config['view']['failed_validation']); if (!is_numeric($config['view']['empty_content'])) { - $config['view']['empty_content'] = constant('\FOS\Rest\Util\Codes::'.$config['view']['empty_content']); + $config['view']['empty_content'] = constant('\FOS\RestBundle\Util\Codes::'.$config['view']['empty_content']); } $container->setParameter($this->getAlias().'.empty_content', $config['view']['empty_content']); @@ -104,7 +104,7 @@ public function load(array $configs, ContainerBuilder $container) foreach ($config['exception']['codes'] as $exception => $code) { if (!is_numeric($code)) { - $config['exception']['codes'][$exception] = constant("\FOS\Rest\Util\Codes::$code"); + $config['exception']['codes'][$exception] = constant("\FOS\RestBundle\Util\Codes::$code"); } $this->testExceptionExists($exception); } diff --git a/EventListener/BodyListener.php b/EventListener/BodyListener.php index d5d4dc5b3..d4f947d0f 100644 --- a/EventListener/BodyListener.php +++ b/EventListener/BodyListener.php @@ -11,7 +11,7 @@ namespace FOS\RestBundle\EventListener; -use FOS\Rest\Decoder\DecoderProviderInterface; +use FOS\RestBundle\Decoder\DecoderProviderInterface; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpKernel\Event\GetResponseEvent; diff --git a/EventListener/FormatListener.php b/EventListener/FormatListener.php index 6299ae602..c44b8a91b 100644 --- a/EventListener/FormatListener.php +++ b/EventListener/FormatListener.php @@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use FOS\RestBundle\Util\FormatNegotiatorInterface; /** diff --git a/EventListener/ViewResponseListener.php b/EventListener/ViewResponseListener.php index c9b329cbe..2060fb319 100644 --- a/EventListener/ViewResponseListener.php +++ b/EventListener/ViewResponseListener.php @@ -22,7 +22,7 @@ use FOS\RestBundle\View\View; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation. diff --git a/Request/ParamFetcher.php b/Request/ParamFetcher.php index 9505bd482..fc1f6c419 100644 --- a/Request/ParamFetcher.php +++ b/Request/ParamFetcher.php @@ -11,7 +11,7 @@ namespace FOS\RestBundle\Request; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Controller\Annotations\Param; use FOS\RestBundle\Controller\Annotations\RequestParam; diff --git a/Request/RequestBodyParamConverter.php b/Request/RequestBodyParamConverter.php index 52692292a..2488064c4 100644 --- a/Request/RequestBodyParamConverter.php +++ b/Request/RequestBodyParamConverter.php @@ -23,7 +23,7 @@ use JMS\Serializer\Exception\Exception as JMSSerializerException; use JMS\Serializer\DeserializationContext; use JMS\Serializer\SerializerInterface; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * @author Tyler Stroud diff --git a/Resources/config/body_listener.xml b/Resources/config/body_listener.xml index 3798e9fc5..8965190cd 100644 --- a/Resources/config/body_listener.xml +++ b/Resources/config/body_listener.xml @@ -6,11 +6,11 @@ - + - + - + %fos_rest.decoders% diff --git a/Resources/doc/2-the-view-layer.md b/Resources/doc/2-the-view-layer.md index 8116c62b6..3eb29896b 100644 --- a/Resources/doc/2-the-view-layer.md +++ b/Resources/doc/2-the-view-layer.md @@ -172,7 +172,7 @@ formats if needed. Finally the HTTP response status code for failed validation defaults to ``400``. Note when changing the default you can use name constants of -``FOS\Rest\Util\Codes`` class or an integer status code. +``FOS\RestBundle\Util\Codes`` class or an integer status code. You can also set the default templating engine to something different than the default of ``twig``: diff --git a/Resources/doc/3-listener-support.md b/Resources/doc/3-listener-support.md index 4ad05fbb6..510bdda7d 100644 --- a/Resources/doc/3-listener-support.md +++ b/Resources/doc/3-listener-support.md @@ -185,7 +185,7 @@ fos_rest: ``` Your custom decoder service must use a class that implements the -``FOS\Rest\Decoder\DecoderInterface``. +``FOS\RestBundle\Decoder\DecoderInterface``. If you want to be able to use form with checkbox and have true and false value (without any issue) you have to use : fos_rest.decoder.jsontoform (available since fosrest 0.8.0) diff --git a/Resources/doc/4-exception-controller-support.md b/Resources/doc/4-exception-controller-support.md index 2d5e1f333..9e8835e1b 100644 --- a/Resources/doc/4-exception-controller-support.md +++ b/Resources/doc/4-exception-controller-support.md @@ -19,7 +19,7 @@ twig: To map Exception classes to HTTP response status codes an “exception map” may be configured, where the keys match a fully qualified class name and the values are either an integer HTTP response status code or a string matching a class -constant of the ``FOS\Rest\Util\Codes`` class: +constant of the ``FOS\RestBundle\Util\Codes`` class: ```yaml # app/config/config.yml diff --git a/Resources/doc/examples/RssHandler.php b/Resources/doc/examples/RssHandler.php index 9d762779c..b6e09a0d7 100644 --- a/Resources/doc/examples/RssHandler.php +++ b/Resources/doc/examples/RssHandler.php @@ -4,7 +4,7 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandler; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use Symfony\Bridge\Monolog\Logger; use Symfony\Component\HttpFoundation\Request; diff --git a/Tests/EventListener/BodyListenerTest.php b/Tests/EventListener/BodyListenerTest.php index 2b727f766..2bbee7a75 100644 --- a/Tests/EventListener/BodyListenerTest.php +++ b/Tests/EventListener/BodyListenerTest.php @@ -35,7 +35,7 @@ class BodyListenerTest extends \PHPUnit_Framework_TestCase */ public function testOnKernelRequest($decode, $request, $method, $contentType, $expectedParameters) { - $decoder = $this->getMockBuilder('FOS\Rest\Decoder\DecoderInterface')->disableOriginalConstructor()->getMock(); + $decoder = $this->getMockBuilder('FOS\RestBundle\Decoder\DecoderInterface')->disableOriginalConstructor()->getMock(); $decoder->expects($this->any()) ->method('decode') ->will($this->returnValue($request->getContent())); diff --git a/Tests/View/ViewHandlerTest.php b/Tests/View/ViewHandlerTest.php index 7ce0496a1..62979a353 100644 --- a/Tests/View/ViewHandlerTest.php +++ b/Tests/View/ViewHandlerTest.php @@ -15,7 +15,7 @@ use FOS\RestBundle\View\View; use FOS\RestBundle\View\ViewHandler; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; diff --git a/Tests/View/ViewTest.php b/Tests/View/ViewTest.php index 019398db9..957053849 100644 --- a/Tests/View/ViewTest.php +++ b/Tests/View/ViewTest.php @@ -15,7 +15,7 @@ use FOS\RestBundle\View\RedirectView; use FOS\RestBundle\View\RouteRedirectView; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; diff --git a/UPGRADING.md b/UPGRADING.md index fe2d7c357..b74c0dcec 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -10,6 +10,14 @@ list important BC breaks. View::createRouteRedirect instead. Note: the default status code for a route redirect has changed from HTTP_CREATED (201) to HTTP_FOUND (302). +### upgrading from 1.0.0-RC1 + + * The Bundle no longer depends on "friendsofsymfony/rest" and as a result several class names have changed. + Specifically ``FOS\Rest\Util\Codes`` is now ``FOS\RestBundle\Util\Codes`` and also the sub-namespace for + the decoders has changed from ``FOS\Rest\Decoder`` to ``FOS\RestBundle\Decoder``. In practice it should be + sufficient to simply search replace ``FOS\Rest\`` with ``FOS\RestBundle\`. + * The XmlDecoder now has a dependency on "symfony/serializer" + ### upgrading from 0.13.1 * ExceptionController::showAction() doesn't have type hint on the $exception object anymore due to a BC change diff --git a/Util/Codes.php b/Util/Codes.php new file mode 100644 index 000000000..01ae9ac82 --- /dev/null +++ b/Util/Codes.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\RestBundle\Util; + +/** + * List of HTTP response status codes. + * + * The list of codes is complete according to the + * {@link http://www.iana.org/assignments/http-status-codes/ Hypertext Transfer Protocol (HTTP) Status Code Registry} + * (last updated 2012-02-13). + * + * Unless otherwise noted, the status code is defined in RFC2616. + * + * @author Jordi Boggiano + * @author Markus Lanthaler + */ +final class Codes +{ + const HTTP_CONTINUE = 100; + const HTTP_SWITCHING_PROTOCOLS = 101; + const HTTP_PROCESSING = 102; // RFC2518 + const HTTP_OK = 200; + const HTTP_CREATED = 201; + const HTTP_ACCEPTED = 202; + const HTTP_NON_AUTHORITATIVE_INFORMATION = 203; + const HTTP_NO_CONTENT = 204; + const HTTP_RESET_CONTENT = 205; + const HTTP_PARTIAL_CONTENT = 206; + const HTTP_MULTI_STATUS = 207; // RFC4918 + const HTTP_ALREADY_REPORTED = 208; // RFC5842 + const HTTP_IM_USED = 226; // RFC3229 + const HTTP_MULTIPLE_CHOICES = 300; + const HTTP_MOVED_PERMANENTLY = 301; + const HTTP_FOUND = 302; + const HTTP_SEE_OTHER = 303; + const HTTP_NOT_MODIFIED = 304; + const HTTP_USE_PROXY = 305; + const HTTP_RESERVED = 306; + const HTTP_TEMPORARY_REDIRECT = 307; + const HTTP_PERMANENTLY_REDIRECT = 308; // RFC-reschke-http-status-308-07 + const HTTP_BAD_REQUEST = 400; + const HTTP_UNAUTHORIZED = 401; + const HTTP_PAYMENT_REQUIRED = 402; + const HTTP_FORBIDDEN = 403; + const HTTP_NOT_FOUND = 404; + const HTTP_METHOD_NOT_ALLOWED = 405; + const HTTP_NOT_ACCEPTABLE = 406; + const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407; + const HTTP_REQUEST_TIMEOUT = 408; + const HTTP_CONFLICT = 409; + const HTTP_GONE = 410; + const HTTP_LENGTH_REQUIRED = 411; + const HTTP_PRECONDITION_FAILED = 412; + const HTTP_REQUEST_ENTITY_TOO_LARGE = 413; + const HTTP_REQUEST_URI_TOO_LONG = 414; + const HTTP_UNSUPPORTED_MEDIA_TYPE = 415; + const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; + const HTTP_EXPECTATION_FAILED = 417; + const HTTP_I_AM_A_TEAPOT = 418; // RFC2324 + const HTTP_UNPROCESSABLE_ENTITY = 422; // RFC4918 + const HTTP_LOCKED = 423; // RFC4918 + const HTTP_FAILED_DEPENDENCY = 424; // RFC4918 + const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817 + const HTTP_UPGRADE_REQUIRED = 426; // RFC2817 + const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585 + const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585 + const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585 + const HTTP_INTERNAL_SERVER_ERROR = 500; + const HTTP_NOT_IMPLEMENTED = 501; + const HTTP_BAD_GATEWAY = 502; + const HTTP_SERVICE_UNAVAILABLE = 503; + const HTTP_GATEWAY_TIMEOUT = 504; + const HTTP_VERSION_NOT_SUPPORTED = 505; + const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295 + const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918 + const HTTP_LOOP_DETECTED = 508; // RFC5842 + const HTTP_NOT_EXTENDED = 510; // RFC2774 + const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585 +} diff --git a/View/JsonpHandler.php b/View/JsonpHandler.php index ccc067563..e8623358b 100644 --- a/View/JsonpHandler.php +++ b/View/JsonpHandler.php @@ -15,7 +15,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\HttpException; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * Implements a custom handler for JSONP leveraging the ViewHandler diff --git a/View/RedirectView.php b/View/RedirectView.php index ecbdb46c9..aa4d77d4d 100644 --- a/View/RedirectView.php +++ b/View/RedirectView.php @@ -11,7 +11,7 @@ namespace FOS\RestBundle\View; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * Url based redirect implementation. diff --git a/View/RouteRedirectView.php b/View/RouteRedirectView.php index 330ad2dac..5c772f7b8 100644 --- a/View/RouteRedirectView.php +++ b/View/RouteRedirectView.php @@ -11,7 +11,7 @@ namespace FOS\RestBundle\View; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; /** * Route based redirect implementation. diff --git a/View/View.php b/View/View.php index 570b847cc..9a42e7fa5 100644 --- a/View/View.php +++ b/View/View.php @@ -11,7 +11,7 @@ namespace FOS\RestBundle\View; -use FOS\Rest\Util\Codes; +use FOS\RestBundle\Util\Codes; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; use Symfony\Component\HttpFoundation\Response; diff --git a/View/ViewHandler.php b/View/ViewHandler.php index b2bd591c6..07c59bb65 100644 --- a/View/ViewHandler.php +++ b/View/ViewHandler.php @@ -22,8 +22,7 @@ use Symfony\Component\Form\FormInterface; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; -use FOS\Rest\Util\Codes; - +use FOS\RestBundle\Util\Codes; use FOS\RestBundle\Util\ExceptionWrapper; /** diff --git a/composer.json b/composer.json index fca765930..119e3ebb7 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,6 @@ "require": { "php": ">=5.3.2", - "friendsofsymfony/rest": "1.0.*", "symfony/framework-bundle": "~2.1", "doctrine/inflector": "1.0.*", "willdurand/negotiation": "1.0.*" @@ -42,7 +41,7 @@ "suggest": { "sensio/framework-extra-bundle": "Add support for route annotations and the view response listener", "jms/serializer-bundle": "Add support for advanced serialization capabilities, recommended, requires 0.12.*", - "symfony/serializer": "Add support for basic serialization capabilities, requires >=2.0,<2.3-dev" + "symfony/serializer": "Add support for basic serialization capabilities and xml decoding, requires ~2.1" }, "conflict": {