1414use Symfony \Component \Config \FileLocatorInterface ;
1515use Symfony \Component \Routing \Loader \XmlFileLoader ;
1616use Symfony \Component \Routing \RouteCollection ;
17- use Symfony \Component \Routing \Route ;
18-
1917use FOS \RestBundle \Routing \RestRouteCollection ;
20- use FOS \RestBundle \Routing \Loader \RestRouteProcessor ;
21-
2218use Symfony \Component \Config \Util \XmlUtils ;
2319
2420/**
@@ -37,12 +33,23 @@ class RestXmlCollectionLoader extends XmlFileLoader
3733 *
3834 * @param FileLocatorInterface $locator locator
3935 * @param RestRouteProcessor $processor route processor
36+ * @param boolean $includeFormat whether or not the requested view format must be included in the route path
37+ * @param string[] $formats supported view formats
38+ * @param string $defaultFormat default view format
4039 */
41- public function __construct (FileLocatorInterface $ locator , RestRouteProcessor $ processor )
42- {
40+ public function __construct (
41+ FileLocatorInterface $ locator ,
42+ RestRouteProcessor $ processor ,
43+ $ includeFormat = true ,
44+ array $ formats = array (),
45+ $ defaultFormat = null
46+ ) {
4347 parent ::__construct ($ locator );
4448
4549 $ this ->processor = $ processor ;
50+ $ this ->includeFormat = $ includeFormat ;
51+ $ this ->formats = $ formats ;
52+ $ this ->defaultFormat = $ defaultFormat ;
4653 }
4754
4855 /**
@@ -89,6 +96,65 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
8996 }
9097 }
9198
99+ /**
100+ * {@inheritDoc}
101+ */
102+ protected function parseRoute (RouteCollection $ collection , \DOMElement $ node , $ path )
103+ {
104+ // the Symfony Routing component uses a path attribute since Symfony 2.2
105+ // instead of the deprecated pattern attribute0
106+ if (!$ node ->hasAttribute ('path ' )) {
107+ $ node ->setAttribute ('path ' , $ node ->getAttribute ('pattern ' ));
108+ $ node ->removeAttribute ('pattern ' );
109+ }
110+
111+ if ($ this ->includeFormat ) {
112+ $ path = $ node ->getAttribute ('path ' );
113+ // append format placeholder if not present
114+ if (false === strpos ($ path , '{_format} ' )) {
115+ $ node ->setAttribute ('path ' , $ path .'.{_format} ' );
116+ }
117+
118+ // set format requirement if configured globally
119+ $ requirements = $ node ->getElementsByTagNameNS (self ::NAMESPACE_URI , 'requirement ' );
120+ $ format = null ;
121+ for ($ i = 0 ; $ i < $ requirements ->length ; $ i ++) {
122+ $ item = $ requirements ->item ($ i );
123+ if ($ item instanceof \DOMElement && $ item ->hasAttribute ('_format ' )) {
124+ $ format = $ item ->getAttribute ('_format ' );
125+ break ;
126+ }
127+ }
128+ if (null === $ format && !empty ($ this ->formats )) {
129+ $ requirement = $ node ->ownerDocument ->createElementNs (
130+ self ::NAMESPACE_URI ,
131+ 'requirement ' ,
132+ implode ('| ' , array_keys ($ this ->formats ))
133+ );
134+ $ requirement ->setAttribute ('key ' , '_format ' );
135+ $ node ->appendChild ($ requirement );
136+
137+ /*$doc =new \DOMDocument();
138+ $doc->appendChild($doc->importNode($node, true));
139+ echo $doc->saveHTML();*/
140+ }
141+ }
142+
143+ // set the default format if configured
144+ if (null !== $ this ->defaultFormat ) {
145+ $ config ['defaults ' ]['_format ' ] = $ this ->defaultFormat ;
146+ $ defaultFormatNode = $ node ->ownerDocument ->createElementNS (
147+ self ::NAMESPACE_URI ,
148+ 'default ' ,
149+ $ this ->defaultFormat
150+ );
151+ $ defaultFormatNode ->setAttribute ('key ' , '_format ' );
152+ $ node ->appendChild ($ defaultFormatNode );
153+ }
154+
155+ parent ::parseRoute ($ collection , $ node , $ path );
156+ }
157+
92158 /**
93159 * Returns true if this class supports the given resource.
94160 *
0 commit comments