Skip to content

Commit

Permalink
Merge branch '4.4' into feat-switch
Browse files Browse the repository at this point in the history
  • Loading branch information
romaricdrigon committed Oct 15, 2019
2 parents 3d77e4f + 6253369 commit a57d479
Show file tree
Hide file tree
Showing 94 changed files with 1,380 additions and 900 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
`DebugCommand::__construct()` method, swap the variables position.
* the `LintCommand` lints all the templates stored in all configured Twig paths if none argument is provided
* deprecated accepting STDIN implicitly when using the `lint:twig` command, use `lint:twig -` (append a dash) instead to make it explicit.
* added `--show-deprecations` option to the `lint:twig` command
* added support for Bootstrap4 switches, use `switch-custom` as `label_attr` in a `CheckboxType`

4.3.0
Expand Down
25 changes: 24 additions & 1 deletion src/Symfony/Bridge/Twig/Command/LintCommand.php
Expand Up @@ -50,6 +50,7 @@ protected function configure()
$this
->setDescription('Lints a template and outputs encountered errors')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
->addOption('show-deprecations', null, InputOption::VALUE_NONE, 'Show deprecations as errors')
->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command lints a template and outputs to STDOUT
Expand Down Expand Up @@ -77,6 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$filenames = $input->getArgument('filename');
$showDeprecations = $input->getOption('show-deprecations');

if (['-'] === $filenames) {
return $this->display($input, $output, $io, [$this->validate($this->getStdin(), uniqid('sf_', true))]);
Expand Down Expand Up @@ -104,7 +106,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$filesInfo = $this->getFilesInfo($filenames);
if ($showDeprecations) {
$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
if (E_USER_DEPRECATED === $level) {
$templateLine = 0;
if (preg_match('/ at line (\d+) /', $message, $matches)) {
$templateLine = $matches[1];
}

throw new Error($message, $templateLine);
}

return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
});
}

try {
$filesInfo = $this->getFilesInfo($filenames);
} finally {
if ($showDeprecations) {
restore_error_handler();
}
}

return $this->display($input, $output, $io, $filesInfo);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Expand Up @@ -20,7 +20,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
use Symfony\Component\ErrorHandler\Exception\ErrorException;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -208,7 +208,7 @@ private function renderRegistrationErrors(InputInterface $input, OutputInterface

foreach ($this->registrationErrors as $error) {
if (!$error instanceof \Exception) {
$error = new FatalThrowableError($error);
$error = new ErrorException($error);
}

$this->doRenderException($error, $output);
Expand Down
Expand Up @@ -286,6 +286,25 @@ protected function sortTaggedServicesByPriority(array $services): array
return array_keys($maxPriority);
}

protected function sortTagsByPriority(array $tags): array
{
$sortedTags = [];
foreach ($tags as $tagName => $tag) {
$sortedTags[$tagName] = $this->sortByPriority($tag);
}

return $sortedTags;
}

protected function sortByPriority(array $tag): array
{
usort($tag, function ($a, $b) {
return ($b['priority'] ?? 0) <=> ($a['priority'] ?? 0);
});

return $tag;
}

/**
* Gets class description from a docblock.
*/
Expand Down
Expand Up @@ -270,7 +270,7 @@ private function getContainerDefinitionData(Definition $definition, bool $omitTa

if (!$omitTags) {
$data['tags'] = [];
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($this->sortTagsByPriority($definition->getTags()) as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$data['tags'][] = ['name' => $tagName, 'parameters' => $parameters];
}
Expand Down
Expand Up @@ -233,7 +233,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
}

if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($this->sortTagsByPriority($definition->getTags()) as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$output .= "\n".'- Tag: `'.$tagName.'`';
foreach ($parameters as $name => $value) {
Expand Down
Expand Up @@ -238,7 +238,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$styledServiceId = $rawOutput ? $serviceId : sprintf('<fg=cyan>%s</fg=cyan>', OutputFormatter::escape($serviceId));
if ($definition instanceof Definition) {
if ($showTag) {
foreach ($definition->getTag($showTag) as $key => $tag) {
foreach ($this->sortByPriority($definition->getTag($showTag)) as $key => $tag) {
$tagValues = [];
foreach ($tagsNames as $tagName) {
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : '';
Expand Down
Expand Up @@ -371,7 +371,7 @@ private function getContainerDefinitionDocument(Definition $definition, string $
}

if (!$omitTags) {
if ($tags = $definition->getTags()) {
if ($tags = $this->sortTagsByPriority($definition->getTags())) {
$serviceXML->appendChild($tagsXML = $dom->createElement('tags'));
foreach ($tags as $tagName => $tagData) {
foreach ($tagData as $parameters) {
Expand Down
Expand Up @@ -14,16 +14,16 @@
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"attr2": "val2",
"priority": 0
"attr3": "val3",
"priority": 40
}
},
{
"name": "tag1",
"parameters": {
"attr3": "val3",
"priority": 40
"attr1": "val1",
"attr2": "val2",
"priority": 0
}
}
]
Expand Down Expand Up @@ -87,4 +87,4 @@
},
"aliases": [],
"services": []
}
}
Expand Up @@ -15,13 +15,13 @@ Definitions
- Autowired: no
- Autoconfigured: no
- File: `/path/to/file`
- Tag: `tag1`
- Attr3: val3
- Priority: 40
- Tag: `tag1`
- Attr1: val1
- Attr2: val2
- Priority: 0
- Tag: `tag1`
- Attr3: val3
- Priority: 40

### definition_1

Expand Down
Expand Up @@ -5,8 +5,8 @@
-------------- ------- ------- ---------- ------- -----------------------
 Service ID   attr1   attr2   priority   attr3   Class name 
-------------- ------- ------- ---------- ------- -----------------------
definition_3 val1 val2 0 Full\Qualified\Class3
" 40 val3
definition_3 40 val3 Full\Qualified\Class3
" val1 val2 0
definition_1 val1 30 Full\Qualified\Class1
" val2
definition_2 val1 val2 -20 Full\Qualified\Class2
Expand Down
Expand Up @@ -2,15 +2,15 @@
<container>
<definition id="definition_3" class="Full\Qualified\Class3" public="true" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="/path/to/file">
<tags>
<tag name="tag1">
<parameter name="attr3">val3</parameter>
<parameter name="priority">40</parameter>
</tag>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
<parameter name="attr2">val2</parameter>
<parameter name="priority">0</parameter>
</tag>
<tag name="tag1">
<parameter name="attr3">val3</parameter>
<parameter name="priority">40</parameter>
</tag>
</tags>
</definition>
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="/path/to/file">
Expand Down
45 changes: 28 additions & 17 deletions src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php
Expand Up @@ -133,12 +133,18 @@ abstract protected function doSave(array $values, ?int $lifetime, array $addTagD
/**
* Removes multiple items from the pool and their corresponding tags.
*
* @param array $ids An array of identifiers that should be removed from the pool
* @param array $tagData Optional array of tag identifiers => key identifiers that should be removed from the pool
* @param array $ids An array of identifiers that should be removed from the pool
*
* @return bool True if the items were successfully removed, false otherwise
*/
abstract protected function doDelete(array $ids, array $tagData = []): bool;
abstract protected function doDelete(array $ids);

/**
* Removes relations between tags and deleted items.
*
* @param array $tagData Array of tag => key identifiers that should be removed from the pool
*/
abstract protected function doDeleteTagRelations(array $tagData): bool;

/**
* Invalidates cached items using tags.
Expand All @@ -149,12 +155,22 @@ abstract protected function doDelete(array $ids, array $tagData = []): bool;
*/
abstract protected function doInvalidate(array $tagIds): bool;

/**
* Delete items and yields the tags they were bound to.
*/
protected function doDeleteYieldTags(array $ids): iterable
{
foreach ($this->doFetch($ids) as $id => $value) {
yield $id => \is_array($value) && \is_array($value['tags'] ?? null) ? $value['tags'] : [];
}

$this->doDelete($ids);
}

/**
* {@inheritdoc}
*
* @return bool
*/
public function commit()
public function commit(): bool
{
$ok = true;
$byLifetime = $this->mergeByLifetime;
Expand Down Expand Up @@ -213,17 +229,14 @@ public function commit()

/**
* {@inheritdoc}
*
* Overloaded in order to deal with tags for adjusted doDelete() signature.
*
* @return bool
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
if (!$keys) {
return true;
}

$ok = true;
$ids = [];
$tagData = [];

Expand All @@ -233,24 +246,22 @@ public function deleteItems(array $keys)
}

try {
foreach ($this->doFetch($ids) as $id => $value) {
foreach ($value['tags'] ?? [] as $tag) {
foreach ($this->doDeleteYieldTags(array_values($ids)) as $id => $tags) {
foreach ($tags as $tag) {
$tagData[$this->getId(self::TAGS_PREFIX.$tag)][] = $id;
}
}
} catch (\Exception $e) {
// ignore unserialization failures
$ok = false;
}

try {
if ($this->doDelete(array_values($ids), $tagData)) {
if ((!$tagData || $this->doDeleteTagRelations($tagData)) && $ok) {
return true;
}
} catch (\Exception $e) {
}

$ok = true;

// When bulk-delete failed, retry each item individually
foreach ($ids as $key => $id) {
try {
Expand Down

0 comments on commit a57d479

Please sign in to comment.