Skip to content

Commit 252cbcc

Browse files
committed
replace single colon syntax also for service router loader
1 parent fd71481 commit 252cbcc

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6464
$loader->load(function (ContainerBuilder $container) use ($loader) {
6565
$container->loadFromExtension('framework', array(
6666
'router' => array(
67-
'resource' => 'kernel:loadRoutes',
67+
'resource' => 'kernel::loadRoutes',
6868
'type' => 'service',
6969
),
7070
));

src/Symfony/Bundle/FrameworkBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/polyfill-mbstring": "~1.0",
2828
"symfony/filesystem": "~3.4|~4.0",
2929
"symfony/finder": "~3.4|~4.0",
30-
"symfony/routing": "^3.4.5|^4.0.5"
30+
"symfony/routing": "^4.1"
3131
},
3232
"require-dev": {
3333
"doctrine/cache": "~1.0",

src/Symfony/Component/Routing/Loader/ObjectRouteLoader.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,17 @@ abstract protected function getServiceObject($id);
4444
*/
4545
public function load($resource, $type = null)
4646
{
47-
$parts = explode(':', $resource);
47+
if (1 === substr_count($resource, ':')) {
48+
$resource = str_replace(':', '::', $resource);
49+
@trigger_error(sprintf(
50+
'Referencing service route loaders with a single colon is deprecated since version 4.1 and will be removed in 5.0. Use %s instead.',
51+
$resource
52+
), E_USER_DEPRECATED);
53+
}
54+
55+
$parts = explode('::', $resource);
4856
if (2 != count($parts)) {
49-
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
57+
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service::method"', $resource));
5058
}
5159

5260
$serviceString = $parts[0];
@@ -58,7 +66,7 @@ public function load($resource, $type = null)
5866
throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', get_class($this), gettype($loaderObject)));
5967
}
6068

61-
if (!method_exists($loaderObject, $method)) {
69+
if (!is_callable(array($loaderObject, $method)))q {
6270
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, get_class($loaderObject), $resource));
6371
}
6472

src/Symfony/Component/Routing/Tests/Loader/ObjectRouteLoaderTest.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
class ObjectRouteLoaderTest extends TestCase
2020
{
21-
public function testLoadCallsServiceAndReturnsCollection()
21+
/**
22+
* @group legacy
23+
* @expectedDeprecation Referencing service route loaders with a single colon is deprecated since version 4.1 and will be removed in 5.0. Use my_route_provider_service::loadRoutes instead.
24+
*/
25+
public function testLoadCallsServiceAndReturnsCollectionWithLegacyNotation()
2226
{
2327
$loader = new ObjectRouteLoaderForTest();
2428

@@ -40,6 +44,28 @@ public function testLoadCallsServiceAndReturnsCollection()
4044
$this->assertNotEmpty($actualRoutes->getResources());
4145
}
4246

47+
public function testLoadCallsServiceAndReturnsCollection()
48+
{
49+
$loader = new ObjectRouteLoaderForTest();
50+
51+
// create a basic collection that will be returned
52+
$collection = new RouteCollection();
53+
$collection->add('foo', new Route('/foo'));
54+
55+
$loader->loaderMap = array(
56+
'my_route_provider_service' => new RouteService($collection),
57+
);
58+
59+
$actualRoutes = $loader->load(
60+
'my_route_provider_service::loadRoutes',
61+
'service'
62+
);
63+
64+
$this->assertSame($collection, $actualRoutes);
65+
// the service file should be listed as a resource
66+
$this->assertNotEmpty($actualRoutes->getResources());
67+
}
68+
4369
/**
4470
* @expectedException \InvalidArgumentException
4571
* @dataProvider getBadResourceStrings
@@ -54,7 +80,6 @@ public function getBadResourceStrings()
5480
{
5581
return array(
5682
array('Foo'),
57-
array('Bar::baz'),
5883
array('Foo:Bar:baz'),
5984
);
6085
}
@@ -66,7 +91,7 @@ public function testExceptionOnNoObjectReturned()
6691
{
6792
$loader = new ObjectRouteLoaderForTest();
6893
$loader->loaderMap = array('my_service' => 'NOT_AN_OBJECT');
69-
$loader->load('my_service:method');
94+
$loader->load('my_service::method');
7095
}
7196

7297
/**
@@ -76,7 +101,7 @@ public function testExceptionOnBadMethod()
76101
{
77102
$loader = new ObjectRouteLoaderForTest();
78103
$loader->loaderMap = array('my_service' => new \stdClass());
79-
$loader->load('my_service:method');
104+
$loader->load('my_service::method');
80105
}
81106

82107
/**
@@ -93,7 +118,7 @@ public function testExceptionOnMethodNotReturningCollection()
93118

94119
$loader = new ObjectRouteLoaderForTest();
95120
$loader->loaderMap = array('my_service' => $service);
96-
$loader->load('my_service:loadRoutes');
121+
$loader->load('my_service::loadRoutes');
97122
}
98123
}
99124

0 commit comments

Comments
 (0)