8000 [Routing][Translation] Assert return value for PHP configuration by ro0NL · Pull Request #23522 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing][Translation] Assert return value for PHP configuration #23522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Symfony/Component/Routing/Loader/PhpFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public function supports($resource, $type = null)
*/
private static function includeFile($file, PhpFileLoader $loader)
{
return include $file;
$collection = include $file;

if (!$collection instanceof RouteCollection) {
throw new \UnexpectedValueException(sprintf('PHP routing configuration must return a RouteCollection, got "%s" in "%s".', is_object($collection) ? get_class($collection) : gettype($collection), $file));
}

return $collection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return array('something');
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ public function testThatDefiningVariableInConfigFileHasNoSideEffects()
(string) $fileResource
);
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessageRegExp /^PHP routing configuration must return a RouteCollection, got "array" in ".+invalid_return\.php"\.$/
*/
public function testInvalidReturn()
{
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('invalid_return.php');
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/Loader/PhpFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function load($resource, $locale, $domain = 'messages')

$messages = require $resource;

if (!is_array($messages)) {
throw new \UnexpectedValueException(sprintf('PHP translation files must return an array, got "%s" in "%s".', is_object($messages) ? get_class($messages) : gettype($messages), $resource));
}

$catalogue = parent::load($messages, $locale, $domain);

if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public function testLoadThrowsAnExceptionIfFileNotLocal()
$resource = 'http://example.com/resources.php';
$loader->load($resource, 'en', 'domain1');
}

/**
* @expectedException \UnexpectedValueException
* @expectedExceptionMessageRegExp /^PHP translation files must return an array, got "stdClass" in ".+invalid-return\.php"\.$/
*/
public function testInvalidReturn()
{
$loader = new PhpFileLoader();
$loader->load(__DIR__.'/../fixtures/invalid-return.php', 'en');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return new \stdClass();
0