Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 0fcd59b

Browse files
committed
All functional tests pass :)
1 parent 0f0ca05 commit 0fcd59b

16 files changed

+248
-132
lines changed

AutoRoute/Adapter/PhpcrOdmAdapter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function createRoute($url, $contentDocument)
8888
$document->setNodeName($segment);
8989
$this->dm->persist($document);
9090
}
91+
$parentDocument = $document;
9192
}
9293

9394
$headRoute = new AutoRoute();

AutoRoute/AutoRouteManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Util\ClassUtils;
1515
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface;
1616
use Metadata\MetadataFactoryInterface;
17+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
1718

1819
/**
1920
* This class is concerned with the automatic creation of route objects.
@@ -88,11 +89,14 @@ private function getUrlsForDocument($document)
8889
$locales = $this->adapter->getLocales($document) ? : array(null);
8990

9091
foreach ($locales as $locale) {
92+
$urlContext = new UrlContext($document, $locale);
93+
9194
if (null !== $locale) {
9295
$this->adapter->translateObject($document, $locale);
9396
}
9497

95-
$urls[] = $this->urlGenerator->generateUrl($document);
98+
99+
$urls[] = $this->urlGenerator->generateUrl($urlContext);
96100
}
97101

98102
return $urls;

AutoRoute/DefunctRouteHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function handleDefunctRoutes($document, OperationStack $operationStack)
4848

4949
foreach ($referrerCollection as $referrer) {
5050
if (!$operationStack->containsRoute($referrer)) {
51-
$canonicalRoutes = $operationStack->getPersistStack();
51+
if (!$canonicalRoutes = $operationStack->getPersistStack()) {
52+
continue;
53+
}
5254
$this->adapter->removeDefunctRoute($referrer, $canonicalRoutes[0]);
5355
}
5456
}

AutoRoute/TokenProvider/AbstractTokenProvider.php

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProvider;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
6+
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
9+
10+
class ContentDateTimeProvider extends ContentMethodProvider
11+
{
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public function provideValue(UrlContext $urlContext, $options)
16+
{
17+
$object = $urlContext->getObject();
18+
$method = $options['method'];
19+
$this->checkMethodExists($object, $method);
20+
21+
$date = $object->$method();
22+
23+
if (!$date instanceof \DateTime) {
24+
throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.',
25+
get_class($object),
26+
$method
27+
));
28+
}
29+
30+
$string = $date->format($options['date_format']);
31+
32+
return $string;
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function configureOptions(OptionsResolverInterface $optionsResolver)
39+
{
40+
parent::configureOptions($optionsResolver);
41+
42+
$optionsResolver->setRequired(array(
43+
'date_format',
44+
));
45+
46+
$optionsResolver->setDefaults(array(
47+
'date_format' => 'Y-m-d',
48+
));
49+
50+
$optionsResolver->setAllowedTypes(array(
51+
'date_format' => 'string',
52+
));
53+
}
54+
}
55+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProvider;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
6+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
8+
9+
class ContentLocaleProvider implements TokenProviderInterface
10+
{
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function provideValue(UrlContext $urlContext, $options)
15+
{
16+
return $urlContext->getLocale();
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function configureOptions(OptionsResolverInterface $optionsResolver)
23+
{
24+
}
25+
}
26+

AutoRoute/TokenProvider/ContentMethodProvider.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\TokenProviderInterface;
66
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
77
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
8+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
89

910
class ContentMethodProvider implements TokenProviderInterface
1011
{
@@ -15,20 +16,26 @@ public function __construct(SlugifierInterface $slugifier)
1516
$this->slugifier = $slugifier;
1617
}
1718

18-
/**
19-
* {@inheritDoc}
20-
*/
21-
public function provideValue($object, $options)
19+
protected function checkMethodExists($object, $method)
2220
{
23-
$method = $options['method'];
24-
2521
if (!method_exists($object, $method)) {
2622
throw new \InvalidArgumentException(sprintf(
2723
'Method "%s" does not exist on object "%s"',
2824
$method,
2925
get_class($object)
3026
));
3127
}
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function provideValue(UrlContext $urlContext, $options)
34+
{
35+
$object = $urlContext->getObject();
36+
$method = $options['method'];
37+
38+
$this->checkMethodExists($object, $method);
3239

3340
$value = $object->$method();
3441

AutoRoute/TokenProviderInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

55
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
67

78
interface TokenProviderInterface
89
{
@@ -15,7 +16,7 @@ interface TokenProviderInterface
1516
*
1617
* @return string
1718
*/
18-
public function provideValue($document, $options);
19+
public function provideValue(UrlContext $urlContext, $options);
1920

2021
/**
2122
* Configure the options for this token provider

AutoRoute/UrlContext.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface;
6+
use Metadata\MetadataFactoryInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
/**
10+
* Class which represents a URL and its associated locale
11+
*
12+
* @author Daniel Leech <[email protected]>
13+
*/
14+
class UrlContext
15+
{
16+
protected $object;
17+
protected $locale;
18+
19+
public function __construct($object, $locale)
20+
{
21+
$this->object = $object;
22+
$this->locale = $locale;
23+
}
24+
25+
public function getObject()
26+
{
27+
return $this->object;
28+
}
29+
30+
public function getLocale()
31+
{
32+
return $this->locale;
33+
}
34+
}

AutoRoute/UrlGenerator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function __construct(
3636
/**
3737
* {@inheritDoc}
3838
*/
39-
public function generateUrl($document)
39+
public function generateUrl(UrlContext $urlContext)
4040
{
41-
$realClassName = $this->driver->getRealClassName(get_class($document));
41+
$realClassName = $this->driver->getRealClassName(get_class($urlContext->getObject()));
4242
$metadata = $this->metadataFactory->getMetadataForClass($realClassName);
4343

4444
$tokenProviderConfigs = $metadata->getTokenProviders();
@@ -52,7 +52,7 @@ public function generateUrl($document)
5252
$optionsResolver = new OptionsResolver();
5353
$tokenProvider->configureOptions($optionsResolver);
5454

55-
$tokens['{' . $name . '}'] = $tokenProvider->provideValue($document, $optionsResolver->resolve($options['options']));
55+
$tokens['{' . $name . '}'] = $tokenProvider->provideValue($urlContext, $optionsResolver->resolve($options['options']));
5656
}
5757

5858
$urlSchema = $metadata->getUrlSchema();
@@ -64,9 +64,9 @@ public function generateUrl($document)
6464
/**
6565
* {@inheritDoc}
6666
*/
67-
public function resolveConflict($document, $url)
67+
public function resolveConflict(UrlContext $urlContext)
6868
{
69-
$realClassName = $this->driver->getRealClassName($document);
69+
$realClassName = $this->driver->getRealClassName($urlContext->getObject());
7070
$metadata = $this->factory->getMetadataForClass($realClassName);
7171

7272
list ($name, $config) = $metadata->getConflictResolverConfig();

0 commit comments

Comments
 (0)