Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aaeb5d9

Browse files
committed
Merge pull request symfony#7 from symfony-cmf/cleanups
lots of cleanups while debugging an unrelated issue
2 parents 6f7787d + 03c4340 commit aaeb5d9

4 files changed

Lines changed: 43 additions & 26 deletions

File tree

Routing/ChainRouter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
99
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1010
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
11+
use Symfony\Component\Routing\RouteCollection;
1112
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1213
use Symfony\Component\HttpKernel\Log\LoggerInterface;
1314

@@ -76,7 +77,7 @@ public function all()
7677
*
7778
* @param string $url
7879
* @throws ResourceNotFoundException $e
79-
* @throws MethodNotAlloedException $e
80+
* @throws MethodNotAllowedException $e
8081
* @return array
8182
*/
8283
public function match($url)
@@ -88,12 +89,12 @@ public function match($url)
8889
return $router->match($url);
8990
} catch (ResourceNotFoundException $e) {
9091
if ($this->logger) {
91-
$this->logger->addError($e);
92+
$this->logger->addInfo($e->getMessage());
9293
}
9394
// Needs special care
9495
} catch (MethodNotAllowedException $e) {
9596
if ($this->logger) {
96-
$this->logger->addError($e);
97+
$this->logger->addInfo($e->getMessage());
9798
}
9899
$methodNotAllowed = $e;
99100
}
@@ -107,7 +108,7 @@ public function match($url)
107108
* It will always return the first route generated.
108109
*
109110
* @param string $name
110-
* @param array $paramaters
111+
* @param array $parameters
111112
* @param Boolean $absolute
112113
* @throws RouteNotFoundException
113114
* @return string
@@ -119,7 +120,7 @@ public function generate($name, $parameters = array(), $absolute = false)
119120
return $router->generate($name, $parameters, $absolute);
120121
} catch (RouteNotFoundException $e) {
121122
if ($this->logger) {
122-
$this->logger->addError($e);
123+
$this->logger->addInfo($e->getMessage());
123124
}
124125
}
125126
}
@@ -158,7 +159,7 @@ public function warmUp($cacheDir)
158159
public function getRouteCollection()
159160
{
160161
// TODO: is this the right thing? can we optimize?
161-
$collection = new \Symfony\Component\Routing\RouteCollection();
162+
$collection = new RouteCollection();
162163
foreach ($this->all() as $router) {
163164
$collection->addCollection($router->getRouteCollection());
164165
}

Routing/DoctrineRouter.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use Symfony\Component\Routing\RouterInterface;
77
use Symfony\Component\Routing\RequestContext;
88
use Symfony\Component\Routing\Exception\RouteNotFoundException;
9-
use Doctrine\Common\Persistence\ObjectManager;
9+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10+
use Symfony\Component\Routing\RouteCollection;
11+
1012
use Symfony\Cmf\Bundle\ChainRoutingBundle\Resolver\ControllerResolverInterface;
1113

14+
use Doctrine\Common\Persistence\ObjectManager;
15+
1216
/**
1317
* A router that reads entries from a Object-Document Mapper store.
1418
*
@@ -30,11 +34,13 @@ class DoctrineRouter implements RouterInterface
3034
* route has one associated
3135
*/
3236
const CONTENT_KEY = 'contentDocument';
37+
3338
/**
3439
* key for the request attribute that contains the template this document
3540
* wants to use
3641
*/
3742
const CONTENT_TEMPLATE = 'contentTemplate';
43+
3844
protected $om;
3945
protected $resolvers;
4046
protected $routeClass;
@@ -70,12 +76,17 @@ public function addControllerResolver(ControllerResolverInterface $resolver)
7076
$this->resolvers[] = $resolver;
7177
}
7278

73-
// inherit doc
79+
/**
80+
* {@inheritDoc}
81+
*/
7482
public function setContext(RequestContext $context)
7583
{
7684
$this->context = $context;
7785
}
78-
// inherit doc
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
7990
public function getContext()
8091
{
8192
return $this->context;
@@ -130,11 +141,13 @@ public function generate($name, $parameters = array(), $absolute = false)
130141
public function getRouteCollection()
131142
{
132143
/* TODO */
133-
return new \Symfony\Component\Routing\RouteCollection();
144+
return new RouteCollection();
134145
}
135146

136147
/**
137148
* Set the doctrine entity or document manager that will know the urls
149+
*
150+
* @param ObjectManager $om
138151
*/
139152
public function setObjectManager(ObjectManager $om)
140153
{
@@ -154,19 +167,19 @@ public function setObjectManager(ObjectManager $om)
154167
* cases, the action to call on that controller is appended, separated with
155168
* two colons.
156169
*
157-
* @throws ResourceNotFoundException If the requested url does not exist in the ODM
158-
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
159-
*
160170
* @param string $url the full requested url. TODO: is locale eaten away or kept too?
161171
*
162172
* @return array as described above
173+
*
174+
* @throws ResourceNotFoundException If the requested url does not exist in the ODM
175+
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
163176
*/
164177
public function match($url)
165178
{
166179
$route = $this->findRouteForUrl($url);
167180

168181
if (! $route instanceof RouteObjectInterface) {
169-
throw new \Symfony\Component\Routing\Exception\ResourceNotFoundException("No entry or not a route at '$url'");
182+
throw new ResourceNotFoundException("No entry or not a route at '$url'");
170183
}
171184

172185
$defaults = $route->getRouteDefaults();
@@ -178,14 +191,14 @@ public function match($url)
178191
if ($controller !== false) break;
179192
}
180193
if (false === $controller) {
181-
throw new \Symfony\Component\Routing\Exception\ResourceNotFoundException("The resolver was not able to determine a controller for '$url'");;
194+
throw new ResourceNotFoundException("The resolver was not able to determine a controller for '$url'");;
182195
}
183196
$defaults['_controller'] = $controller;
184197
}
185198

186199
$defaults[self::CONTENT_KEY] = $route->getRouteContent();
187200
$defaults['path'] = $url; // TODO: get rid of this
188-
$defaults['_route'] = 'whatever'; //FIXME: what is this? without, we get an undefined index in RouterListener::onKernelRequest
201+
$defaults['_route'] = 'chain_router_doctrine_route'.str_replace('/', '_', $url);
189202

190203
return $defaults;
191204
}
@@ -196,7 +209,7 @@ public function match($url)
196209
*
197210
* Overwrite this method for other ODM or ORM repositories.
198211
*
199-
* @param $url The url to find
212+
* @param string $url The url to find
200213
*
201214
* @return the RouteObjectInterface object for this url or null if none is found
202215
*/
@@ -215,15 +228,18 @@ protected function findRouteForUrl($url)
215228
/**
216229
* Called in generate when there is no route given in the parameters
217230
*
218-
* @param $parameters which should contain a content field containing a RouteAwareInterface object
231+
* @param array $parameters which should contain a content field containing a RouteAwareInterface object
232+
*
219233
* @return the route instance
234+
*
220235
* @throws RouteNotFoundException
221236
*/
222237
protected function getRouteFromContent($parameters)
223238
{
224239
if (! isset($parameters['content'])) {
225240
throw new RouteNotFoundException;
226241
}
242+
227243
if (! $parameters['content'] instanceof RouteAwareInterface) {
228244
$hint = is_object($parameters['content']) ? get_class($parameters['content']) : gettype($parameters['content']);
229245
throw new RouteNotFoundException('The content does not implement RouteAwareInterface: ' . $hint);
@@ -237,5 +253,4 @@ protected function getRouteFromContent($parameters)
237253

238254
return reset($routes);
239255
}
240-
241256
}

Tests/Routing/ChainRouterTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Tests\Routing;
44

5-
use Symfony\Cmf\Bundle\ChainRoutingBundle\Routing\ChainRouter;
5+
use Symfony\Component\Routing\RouteCollection;
66

7+
use Symfony\Cmf\Bundle\ChainRoutingBundle\Routing\ChainRouter;
78
use Symfony\Cmf\Bundle\ChainRoutingBundle\Test\CmfUnitTestCase;
89

910
class ChainRouterTest extends CmfUnitTestCase
@@ -252,9 +253,9 @@ public function testWarmup()
252253
public function testRouteCollection()
253254
{
254255
list($low, $high) = $this->createRouterMocks();
255-
$lowcol = new \Symfony\Component\Routing\RouteCollection();
256+
$lowcol = new RouteCollection();
256257
$lowcol->add('low', $this->buildMock('Symfony\\Component\\Routing\\Route'));
257-
$highcol = new \Symfony\Component\Routing\RouteCollection();
258+
$highcol = new RouteCollection();
258259
$highcol->add('high', $this->buildMock('Symfony\\Component\\Routing\\Route'));
259260

260261
$low
@@ -294,4 +295,4 @@ protected function createRouterMocks()
294295

295296
abstract class WarmableRouterMock implements \Symfony\Component\Routing\RouterInterface, \Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface
296297
{
297-
}
298+
}

Tests/Routing/DoctrineRouterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function testMatch()
202202

203203
$expected = array(
204204
'_controller' => 'NameSpace\\Controller::action',
205-
'_route' => 'whatever', //there must be a name to avoid problems with symfony
205+
'_route' => 'chain_router_doctrine_route_company_more',
206206
'contentDocument' => $this->contentDocument,
207207
'path' => $url_alias,
208208
);
@@ -240,7 +240,7 @@ public function testNoReferenceMatch()
240240

241241
$expected = array(
242242
'_controller' => 'NameSpace\\Controller::action',
243-
'_route' => 'whatever', //there must be a name to avoid problems with symfony
243+
'_route' => 'chain_router_doctrine_route_company_more_no_reference',
244244
'contentDocument' => null,
245245
'path' => $url_alias,
246246
'type' => 'found',
@@ -323,4 +323,4 @@ public function getRouteDefaults()
323323
{
324324
return array();
325325
}
326-
}
326+
}

0 commit comments

Comments
 (0)