Skip to content

Commit

Permalink
use latest PHP 8.0 features (#432)
Browse files Browse the repository at this point in the history
* use latest PHP 8.0 features

* fix tests
  • Loading branch information
IonBazan committed Mar 2, 2022
1 parent 0bd566a commit 089c738
Show file tree
Hide file tree
Showing 20 changed files with 316 additions and 295 deletions.
31 changes: 18 additions & 13 deletions Command/DumpCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSJsRoutingBundle package.
*
Expand Down Expand Up @@ -76,28 +78,31 @@ protected function configure(): void
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Specify expose domain',
array()
[]
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if(!in_array($input->getOption('format'), array('js', 'json'))) {
if (!in_array($input->getOption('format'), ['js', 'json'])) {
$output->writeln('<error>Invalid format specified. Use js or json.</error>');

return 1;
}

$callback = $input->getOption('callback');
if(empty($callback)) {
if (empty($callback)) {
$output->writeln('<error>If you include --callback it must not be empty. Do you perhaps want --format=json</error>');

return 1;
}

$output->writeln('Dumping exposed routes.');
$output->writeln('');

$this->doDump($input, $output);

return 0;
}

Expand All @@ -114,28 +119,28 @@ private function doDump(InputInterface $input, OutputInterface $output): void
sprintf(
'%s/web/js/fos_js_routes%s.%s',
$this->projectDir,
empty($domain) ? '' : ('_' . implode('_', $domain)),
empty($domain) ? '' : ('_'.implode('_', $domain)),
$input->getOption('format')
);

if (!is_dir($dir = dirname($targetPath))) {
$output->writeln('<info>[dir+]</info> ' . $dir);
$output->writeln('<info>[dir+]</info> '.$dir);
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException('Unable to create directory ' . $dir);
throw new \RuntimeException('Unable to create directory '.$dir);
}
}

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

$baseUrl = null !== $this->requestContextBaseUrl ?
$this->requestContextBaseUrl :
$this->extractor->getBaseUrl()
;

if ($input->getOption('pretty-print')) {
$params = array('json_encode_options' => JSON_PRETTY_PRINT);
$params = ['json_encode_options' => JSON_PRETTY_PRINT];
} else {
$params = array();
$params = [];
}

$content = $serializer->serialize(
Expand All @@ -153,12 +158,12 @@ private function doDump(InputInterface $input, OutputInterface $output): void
$params
);

if('js' == $input->getOption('format')) {
$content = sprintf("%s(%s);", $input->getOption('callback'), $content);
if ('js' == $input->getOption('format')) {
$content = sprintf('%s(%s);', $input->getOption('callback'), $content);
}

if (false === @file_put_contents($targetPath, $content)) {
throw new \RuntimeException('Unable to write file ' . $targetPath);
throw new \RuntimeException('Unable to write file '.$targetPath);
}
}
}
34 changes: 17 additions & 17 deletions Command/RouterDebugExposedCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSJsRoutingBundle package.
*
Expand All @@ -19,8 +21,8 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

/**
* A console command for retrieving information about exposed routes.
Expand All @@ -36,20 +38,19 @@ public function __construct(private ExposedRoutesExtractorInterface $extractor,
parent::__construct();
}


/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDefinition(array(
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
new InputOption('domain', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify expose domain', array())
))
new InputOption('domain', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify expose domain', []),
])
->setName('fos:js-routing:debug')
->setDescription('Displays currently exposed routes for an application')
->setHelp(<<<EOF
Expand Down Expand Up @@ -81,23 +82,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$helper = new DescriptorHelper();
$helper->describe($output, $route, array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
$helper->describe($output, $route, [
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
));
]);
} else {
$helper = new DescriptorHelper();
$helper->describe($output, $this->getRoutes($input->getOption('domain')), array(
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
$helper->describe($output, $this->getRoutes($input->getOption('domain')), [
'format' => $input->getOption('format'),
'raw_text' => $input->getOption('raw'),
'show_controllers' => $input->getOption('show-controllers'),
));
]);
}

return 0;
}

protected function getRoutes($domain = array()): RouteCollection
protected function getRoutes($domain = []): RouteCollection
{
$routes = $this->extractor->getRoutes();

Expand All @@ -108,14 +110,12 @@ protected function getRoutes($domain = array()): RouteCollection
$targetRoutes = new RouteCollection();

foreach ($routes as $name => $route) {

$expose = $route->getOption('expose');
$expose = is_string($expose) ? ($expose === 'true' ? 'default' : $expose) : 'default';
$expose = is_string($expose) ? ('true' === $expose ? 'default' : $expose) : 'default';

if (in_array($expose, $domain, true)) {
$targetRoutes->add($name, $route);
}

}

return $targetRoutes;
Expand Down
21 changes: 11 additions & 10 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSJsRoutingBundle package.
*
Expand Down Expand Up @@ -33,13 +35,12 @@ class Controller
* 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 ExposedRoutesExtractorInterface $exposedRoutesExtractor the extractor service
* @param bool $debug
*/
public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), private bool $debug = false)
public function __construct(private mixed $serializer, private ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = [], private bool $debug = false)
{
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
}

public function indexAction(Request $request, $_format): Response
Expand All @@ -54,13 +55,13 @@ public function indexAction(Request $request, $_format): Response
$cache = new ConfigCache($this->exposedRoutesExtractor->getCachePath($request->getLocale()), $this->debug);

if (!$cache->isFresh() || $this->debug) {
$exposedRoutes = $this->exposedRoutesExtractor->getRoutes();
$exposedRoutes = $this->exposedRoutesExtractor->getRoutes();
$serializedRoutes = $this->serializer->serialize($exposedRoutes, 'json');
$cache->write($serializedRoutes, $this->exposedRoutesExtractor->getResources());
} else {
$path = method_exists($cache, 'getPath') ? $cache->getPath() : (string) $cache;
$serializedRoutes = file_get_contents($path);
$exposedRoutes = $this->serializer->deserialize(
$exposedRoutes = $this->serializer->deserialize(
$serializedRoutes,
'Symfony\Component\Routing\RouteCollection',
'json'
Expand All @@ -75,7 +76,7 @@ public function indexAction(Request $request, $_format): Response
$this->exposedRoutesExtractor->getPort(),
$this->exposedRoutesExtractor->getScheme(),
$request->getLocale(),
$request->query->has('domain') ? explode(',', $request->query->get('domain')) : array()
$request->query->has('domain') ? explode(',', $request->query->get('domain')) : []
);

$content = $this->serializer->serialize($routesResponse, 'json');
Expand All @@ -86,10 +87,10 @@ public function indexAction(Request $request, $_format): Response
throw new HttpException(400, 'Invalid JSONP callback value');
}

$content = '/**/' . $callback . '(' . $content . ');';
$content = '/**/'.$callback.'('.$content.');';
}

$response = new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
$response = new Response($content, 200, ['Content-Type' => $request->getMimeType($_format)]);
$this->cacheControlConfig->apply($response);

return $response;
Expand Down
12 changes: 7 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSJsRoutingBundle package.
*
Expand All @@ -11,8 +13,8 @@

namespace FOS\JsRoutingBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Configuration class.
Expand All @@ -37,8 +39,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('serializer')->cannotBeEmpty()->end()
->arrayNode('routes_to_expose')
->beforeNormalization()
->ifTrue(function ($v) { return !is_array($v); })
->then(function ($v) { return array($v); })
->ifTrue(fn ($v) => !is_array($v))
->then(fn ($v) => [$v])
->end()
->prototype('scalar')->end()
->end()
Expand All @@ -52,8 +54,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('smaxage')->defaultNull()->end()
->arrayNode('vary')
->beforeNormalization()
->ifTrue(function ($v) { return !is_array($v); })
->then(function ($v) { return array($v); })
->ifTrue(fn ($v) => !is_array($v))
->then(fn ($v) => [$v])
->end()
->prototype('scalar')->end()
->end()
Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/FOSJsRoutingExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the FOSJsRoutingBundle package.
*
Expand All @@ -11,8 +13,8 @@

namespace FOS\JsRoutingBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -55,7 +57,7 @@ public function load(array $configs, ContainerBuilder $container): void
if (isset($config['cache_control'])) {
$config['cache_control']['enabled'] = true;
} else {
$config['cache_control'] = array('enabled' => false);
$config['cache_control'] = ['enabled' => false];
}

$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);
Expand Down
Loading

0 comments on commit 089c738

Please # to comment.