Skip to content

Commit 9334e8f

Browse files
weaverryantobias-93
authored andcommitted
Symfony 4 support (#300)
* Attempting Symfony 4 support * testing beta versions * Removing Symfony 2.3 support and not extending the base RouterDebugCommand RouterDebugCommand became final in Symfony 3.4. But it was only extended for 2.3 support anyways. * Explicitly making public services public ... so that they remain public in Symfony 4.0 * using latest LTS * requested changes * registering commands as services and using DI instead of container * re-allowing Symfony 2.7 and making sure we don't allow PHP version 8 yet ;) * updating last test * trying to test with simple-phpunit * Fix PHPUnit configuration * Revert execute permissions on phpunit command * shortening phpdoc
1 parent a072d48 commit 9334e8f

File tree

10 files changed

+129
-168
lines changed

10 files changed

+129
-168
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ matrix:
66
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
77
dist: precise
88
- php: 7.1
9+
- php: 7.2
910
- php: 7.1
1011
env: SYMFONY_VERSION='2.8.*'
12+
- php: 7.1
13+
env: SYMFONY_VERSION='3.4.*'
14+
- php: 7.1
15+
env: DEPENDENCIES=beta
1116

1217
sudo: false
1318

@@ -17,6 +22,7 @@ cache:
1722
- $HOME/.composer/cache/files
1823

1924
before_install:
25+
- if [ "$DEPENDENCIES" = "beta" ]; then composer config minimum-stability beta; fi;
2026
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then phpenv config-rm xdebug.ini; fi
2127
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/symfony=$SYMFONY_VERSION; fi
2228

Command/DumpCommand.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,58 @@
1111

1212
namespace FOS\JsRoutingBundle\Command;
1313

14+
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
1415
use FOS\JsRoutingBundle\Response\RoutesResponse;
15-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16+
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Serializer\SerializerInterface;
1921

2022
/**
2123
* Dumps routes to the filesystem.
2224
*
2325
* @author Benjamin Dulau <[email protected]>
2426
*/
25-
class DumpCommand extends ContainerAwareCommand
27+
class DumpCommand extends Command
2628
{
29+
protected static $defaultName = 'fos:js-routing:dump';
30+
31+
/**
32+
* @var string
33+
*/
34+
private $targetPath;
35+
36+
/**
37+
* @var ExposedRoutesExtractorInterface
38+
*/
39+
private $extractor;
40+
41+
/**
42+
* @var SerializerInterface
43+
*/
44+
private $serializer;
45+
46+
/**
47+
* @var string
48+
*/
49+
private $rootDir;
50+
51+
/**
52+
* @var string
53+
*/
54+
private $requestContextBaseUrl;
55+
56+
public function __construct(ExposedRoutesExtractorInterface $extractor, SerializerInterface $serializer, $rootDir, $requestContextBaseUrl = null)
57+
{
58+
$this->extractor = $extractor;
59+
$this->serializer = $serializer;
60+
$this->rootDir = $rootDir;
61+
$this->requestContextBaseUrl = $requestContextBaseUrl;
62+
63+
parent::__construct();
64+
}
65+
2766
protected function configure()
2867
{
2968
$this
@@ -93,12 +132,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
93132
*/
94133
private function doDump(InputInterface $input, OutputInterface $output)
95134
{
96-
$extractor = $this->getContainer()->get('fos_js_routing.extractor');
97-
$serializer = $this->getContainer()->get('fos_js_routing.serializer');
135+
$extractor = $this->extractor;
136+
$serializer = $this->serializer;
98137
$targetPath = $input->getOption('target') ?:
99138
sprintf(
100139
'%s/../web/js/fos_js_routes.%s',
101-
$this->getContainer()->getParameter('kernel.root_dir'),
140+
$this->rootDir,
102141
$input->getOption('format')
103142
);
104143

@@ -111,9 +150,9 @@ private function doDump(InputInterface $input, OutputInterface $output)
111150

112151
$output->writeln('<info>[file+]</info> ' . $targetPath);
113152

114-
$baseUrl = $this->getContainer()->hasParameter('fos_js_routing.request_context_base_url') ?
115-
$this->getContainer()->getParameter('fos_js_routing.request_context_base_url') :
116-
$extractor->getBaseUrl()
153+
$baseUrl = null !== $this->requestContextBaseUrl ?
154+
$this->requestContextBaseUrl :
155+
$this->extractor->getBaseUrl()
117156
;
118157

119158
if ($input->getOption('pretty-print')) {

Command/RouterDebugExposedCommand.php

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,50 @@
1212
namespace FOS\JsRoutingBundle\Command;
1313

1414
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
15-
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
1615
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Input\InputArgument;
1718
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Input\InputOption;
1820
use Symfony\Component\Console\Output\OutputInterface;
1921
use Symfony\Component\Routing\Route;
22+
use Symfony\Component\Routing\RouterInterface;
2023

2124
/**
2225
* A console command for retrieving information about exposed routes.
2326
*
2427
* @author William DURAND <[email protected]>
2528
*/
26-
class RouterDebugExposedCommand extends RouterDebugCommand
29+
class RouterDebugExposedCommand extends Command
2730
{
31+
protected static $defaultName = 'fos:js-routing:debug';
32+
33+
private $extractor;
34+
35+
private $router;
36+
37+
public function __construct(ExposedRoutesExtractorInterface $extractor, RouterInterface $router)
38+
{
39+
$this->extractor = $extractor;
40+
$this->router = $router;
41+
42+
parent::__construct();
43+
}
44+
45+
2846
/**
2947
* {@inheritdoc}
3048
*/
3149
protected function configure()
3250
{
33-
parent::configure();
34-
3551
$this
52+
->setDefinition(array(
53+
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
54+
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
55+
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
56+
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
57+
))
3658
->setName('fos:js-routing:debug')
37-
->setAliases(array()) // reset the aliases used by the parent command in Symfony 2.6+
3859
->setDescription('Displays currently exposed routes for an application')
3960
->setHelp(<<<EOF
4061
The <info>fos:js-routing:debug</info> command displays an application's routes which will be available via JavaScript.
@@ -55,44 +76,31 @@ protected function configure()
5576
*/
5677
protected function execute(InputInterface $input, OutputInterface $output)
5778
{
58-
/** @var ExposedRoutesExtractorInterface $extractor */
59-
$extractor = $this->getContainer()->get('fos_js_routing.extractor');
60-
6179
if ($name = $input->getArgument('name')) {
6280
/** @var Route $route */
63-
$route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
81+
$route = $this->router->getRouteCollection()->get($name);
6482

6583
if (!$route) {
6684
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
6785
}
6886

69-
if (!$extractor->isRouteExposed($route, $name)) {
87+
if (!$this->extractor->isRouteExposed($route, $name)) {
7088
throw new \InvalidArgumentException(sprintf('The route "%s" was found, but it is not an exposed route.', $name));
7189
}
7290

73-
if (!class_exists('Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper')) {
74-
// BC layer for Symfony 2.3
75-
$this->outputRoute($output, $name);
76-
} else {
77-
$helper = new DescriptorHelper();
78-
$helper->describe($output, $route, array(
79-
'format' => $input->getOption('format'),
80-
'raw_text' => $input->getOption('raw'),
81-
'show_controllers' => $input->getOption('show-controllers'),
82-
));
83-
}
91+
$helper = new DescriptorHelper();
92+
$helper->describe($output, $route, array(
93+
'format' => $input->getOption('format'),
94+
'raw_text' => $input->getOption('raw'),
95+
'show_controllers' => $input->getOption('show-controllers'),
96+
));
8497
} else {
85-
if (!class_exists('Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper')) {
86-
// BC layer for Symfony 2.3
87-
$this->outputRoutes($output, $extractor->getRoutes());
88-
} else {
89-
$helper = new DescriptorHelper();
90-
$helper->describe($output, $extractor->getRoutes(), array(
91-
'format' => $input->getOption('format'),
92-
'raw_text' => $input->getOption('raw'),
93-
'show_controllers' => $input->getOption('show-controllers'),
94-
));
95-
}
98+
$helper = new DescriptorHelper();
99+
$helper->describe($output, $this->extractor->getRoutes(), array(
100+
'format' => $input->getOption('format'),
101+
'raw_text' => $input->getOption('raw'),
102+
'show_controllers' => $input->getOption('show-controllers'),
103+
));
96104
}
97105
}
98106
}

DependencyInjection/FOSJsRoutingExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ public function load(array $configs, ContainerBuilder $container)
5353
->getDefinition('fos_js_routing.extractor')
5454
->replaceArgument(1, $config['routes_to_expose']);
5555

56-
if (isset($config['request_context_base_url'])) {
57-
$container->setParameter('fos_js_routing.request_context_base_url', $config['request_context_base_url']);
58-
}
56+
$container->setParameter(
57+
'fos_js_routing.request_context_base_url',
58+
$config['request_context_base_url'] ? $config['request_context_base_url'] : null
59+
);
5960

6061
if (isset($config['cache_control'])) {
6162
$config['cache_control']['enabled'] = true;

Resources/config/controllers.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parameter key="fos_js_routing.controller.class">FOS\JsRoutingBundle\Controller\Controller</parameter>
77
</parameters>
88
<services>
9-
<service id="fos_js_routing.controller" class="%fos_js_routing.controller.class%">
9+
<service id="fos_js_routing.controller" class="%fos_js_routing.controller.class%" public="true">
1010
<argument type="service" id="fos_js_routing.serializer" />
1111
<argument type="service" id="fos_js_routing.extractor" />
1212
<argument>%fos_js_routing.cache_control%</argument>

Resources/config/services.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@
88
</parameters>
99

1010
<services>
11-
<service id="fos_js_routing.extractor" class="%fos_js_routing.extractor.class%">
11+
<service id="fos_js_routing.extractor" class="%fos_js_routing.extractor.class%" public="true">
1212
<argument type="service" id="fos_js_routing.router" />
1313
<argument></argument>
1414
<argument>%kernel.cache_dir%</argument>
1515
<argument>%kernel.bundles%</argument>
1616
</service>
17+
18+
<service id="fos_js_routing.dump_command" class="FOS\JsRoutingBundle\Command\DumpCommand" public="false">
19+
<argument type="service" id="fos_js_routing.extractor" />
20+
<argument type="service" id="fos_js_routing.serializer" />
21+
<argument>%kernel.root_dir%</argument>
22+
<argument>%fos_js_routing.request_context_base_url%</argument>
23+
<tag name="console.command" />
24+
</service>
25+
<service id="fos_js_routing.router_debug_exposed_command" class="FOS\JsRoutingBundle\Command\RouterDebugExposedCommand" public="false">
26+
<argument type="service" id="fos_js_routing.extractor" />
27+
<argument type="service" id="router" />
28+
<tag name="console.command" />
29+
</service>
1730
</services>
1831
</container>

0 commit comments

Comments
 (0)