Skip to content
Open
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
14 changes: 11 additions & 3 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ class DumpCommand extends Command
*/
private $requestContextBaseUrl;

public function __construct(ExposedRoutesExtractorInterface $extractor, SerializerInterface $serializer, $rootDir, $requestContextBaseUrl = null)
/**
* @var bool
*/
private $exposeOptions;

public function __construct(ExposedRoutesExtractorInterface $extractor, SerializerInterface $serializer, $rootDir, $requestContextBaseUrl = null, $exposeOptions = false)
{
$this->extractor = $extractor;
$this->serializer = $serializer;
$this->rootDir = $rootDir;
$this->requestContextBaseUrl = $requestContextBaseUrl;
$this->exposeOptions = $exposeOptions;

parent::__construct();
}
Expand Down Expand Up @@ -140,7 +146,7 @@ private function doDump(InputInterface $input, OutputInterface $output)
$this->rootDir,
$input->getOption('format')
);

if (!is_dir($dir = dirname($targetPath))) {
$output->writeln('<info>[dir+]</info> ' . $dir);
if (false === @mkdir($dir, 0777, true)) {
Expand Down Expand Up @@ -168,7 +174,9 @@ private function doDump(InputInterface $input, OutputInterface $output)
$extractor->getPrefix($input->getOption('locale')),
$extractor->getHost(),
$extractor->getPort(),
$extractor->getScheme()
$extractor->getScheme(),
null,
$this->exposeOptions
),
'json',
$params
Expand Down
12 changes: 10 additions & 2 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ class Controller
*/
protected $debug;

/**
* @var boolean
*/
private $exposeRouteOptions;

/**
* Default constructor.
*
* @param object $serializer Any object with a serialize($data, $format) method
* @param ExposedRoutesExtractorInterface $exposedRoutesExtractor The extractor service.
* @param array $cacheControl
* @param boolean $debug
* @param boolean $exposeRouteOptions
*/
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug = false)
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug = false, $exposeRouteOptions = false)
{
$this->serializer = $serializer;
$this->exposedRoutesExtractor = $exposedRoutesExtractor;
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
$this->debug = $debug;
$this->exposeRouteOptions = $exposeRouteOptions;
}

/**
Expand Down Expand Up @@ -98,7 +105,8 @@ public function indexAction(Request $request, $_format)
$this->exposedRoutesExtractor->getHost(),
$this->exposedRoutesExtractor->getPort(),
$this->exposedRoutesExtractor->getScheme(),
$request->getLocale()
$request->getLocale(),
$this->exposeRouteOptions
);

$content = $this->serializer->serialize($routesResponse, 'json');
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->booleanNode('expose_options')->defaultFalse()->end()
->end();

return $builder;
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/FOSJsRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ public function load(array $configs, ContainerBuilder $container)
}

$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);
$container->setParameter('fos_js_routing.expose_options', $config['expose_options']);
}
}
1 change: 1 addition & 0 deletions Resources/config/controllers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument type="service" id="fos_js_routing.extractor" />
<argument>%fos_js_routing.cache_control%</argument>
<argument>%kernel.debug%</argument>
<argument>%fos_js_routing.expose_options%</argument>
</service>
</services>
</container>
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<argument type="service" id="fos_js_routing.serializer" />
<argument>%kernel.root_dir%</argument>
<argument>%fos_js_routing.request_context_base_url%</argument>
<argument>%fos_js_routing.expose_options%</argument>
<tag name="console.command" />
</service>
<service id="fos_js_routing.router_debug_exposed_command" class="FOS\JsRoutingBundle\Command\RouterDebugExposedCommand">
Expand Down
21 changes: 14 additions & 7 deletions Response/RoutesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class RoutesResponse
private $port;
private $scheme;
private $locale;
private $exposeRouteOptions;

public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $port = null, $scheme = null, $locale = null)
public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $port = null, $scheme = null, $locale = null, $exposeRouteOptions = false)
{
$this->baseUrl = $baseUrl;
$this->routes = $routes ?: new RouteCollection();
$this->prefix = $prefix;
$this->host = $host;
$this->baseUrl = $baseUrl;
$this->routes = $routes ?: new RouteCollection();
$this->prefix = $prefix;
$this->host = $host;
$this->port = $port;
$this->scheme = $scheme;
$this->locale = $locale;
$this->scheme = $scheme;
$this->locale = $locale;
$this->exposeRouteOptions = $exposeRouteOptions;
}

public function getBaseUrl()
Expand Down Expand Up @@ -61,6 +63,11 @@ public function getRoutes()
'methods' => $route->getMethods(),
'schemes' => $route->getSchemes(),
);

$options = $route->getOptions();
if ($this->exposeRouteOptions && !empty($options['exposed_options'])) {
$exposedRoutes[$name]['options'] = $options['exposed_options'];
}
}

return $exposedRoutes;
Expand Down
1 change: 1 addition & 0 deletions Tests/Command/DumpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FOS\JsRoutingBundle\Command\DumpCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DumpCommandTest extends TestCase
{
Expand Down
19 changes: 19 additions & 0 deletions Tests/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ public function testIndexActionWithLocalizedRoutes()
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","_locale"],["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":{"_locale":"en"},"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
}

public function testIndexActionWithExposedOptions()
{
$routes = new RouteCollection();
$routes->add('literal', new Route('/homepage'));
$routes->add('blog', new Route('/blog-post/{slug}', array(), array(), array('exposed_options' => array('whatever' => false, 'angular_controller' => 'test')), 'localhost'));

$controller = new Controller(
$this->getSerializer(),
$this->getExtractor($routes),
array(),
false,
true
);

$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]],"options":{"whatever":false,"angular_controller":"test"}}},"prefix":"","host":"","scheme":""}', $response->getContent());
}

public function testConfigCache()
{
$routes = new RouteCollection();
Expand Down
33 changes: 33 additions & 0 deletions Tests/DependencyInjection/FOSJsRoutingExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ public function testLoadSetupsSerializerIfNotGiven()
$this->assertEquals('{"foo":"bar"}', $serializer->serialize(array('foo' => 'bar'), 'json'));
}

public function testExposeOptionsNotSet()
{
$container = $this->load(array());

$this->assertTrue($container->hasParameter('fos_js_routing.expose_options'));
$parameter = $container->getParameter('fos_js_routing.expose_options');

$this->assertFalse($parameter);
}

public function provideExposeOptions()
{
return array(
array(true, true),
array(false, false),
);
}

/**
* @param bool $configValue
* @param bool $expectedParameter
* @dataProvider provideExposeOptions
*/
public function testExposeOptionsSet($configValue, $expectedParameter)
{
$container = $this->load(array(array('expose_options' => $configValue)));

$this->assertTrue($container->hasParameter('fos_js_routing.expose_options'));
$parameter = $container->getParameter('fos_js_routing.expose_options');

$this->assertEquals($expectedParameter, $parameter);
}

private function load(array $configs)
{
$container = new ContainerBuilder();
Expand Down