Skip to content

Commit

Permalink
minor #14515 Do not override PHP constants, only use when available (…
Browse files Browse the repository at this point in the history
…dosten)

This PR was squashed before being merged into the 2.6 branch (closes #14515).

Discussion
----------

Do not override PHP constants, only use when available

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

In #12372, the override of internal constants has been deleted, but the definition of `JSON_PRETTY_PRINT` if not available is missing in that PR.

Commits
-------

d5cc056 Do not override PHP constants, only use when available
  • Loading branch information
fabpot committed May 1, 2015
2 parents 8dcc7e3 + d5cc056 commit 851c7b8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
16 changes: 6 additions & 10 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Expand Up @@ -11,10 +11,6 @@

namespace Symfony\Bridge\Twig\Command;

if (!defined('JSON_PRETTY_PRINT')) {
define('JSON_PRETTY_PRINT', 128);
}

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -40,7 +36,7 @@ public function __construct($name = 'twig:lint')
}

/**
* Sets the twig environment
* Sets the twig environment.
*
* @param \Twig_Environment $twig
*/
Expand Down Expand Up @@ -98,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (!$filename) {
if (0 !== ftell(STDIN)) {
throw new \RuntimeException("Please provide a filename or pipe template content to STDIN.");
throw new \RuntimeException('Please provide a filename or pipe template content to STDIN.');
}

$template = '';
Expand Down Expand Up @@ -190,7 +186,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
}
});

$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
$output->writeln(json_encode($filesInfo, defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0));

return min($errors, 1);
}
Expand All @@ -200,14 +196,14 @@ private function renderException(OutputInterface $output, $template, \Twig_Error
$line = $exception->getTemplateLine();

if ($file) {
$output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
$output->writeln(sprintf('<error>KO</error> in %s (line %s)', $file, $line));
} else {
$output->writeln(sprintf("<error>KO</error> (line %s)", $line));
$output->writeln(sprintf('<error>KO</error> (line %s)', $line));
}

foreach ($this->getContext($template, $line) as $no => $code) {
$output->writeln(sprintf(
"%s %-6s %s",
'%s %-6s %s',
$no == $line ? '<error>>></error>' : ' ',
$no,
$code
Expand Down
Expand Up @@ -11,10 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

if (!defined('JSON_PRETTY_PRINT')) {
define('JSON_PRETTY_PRINT', 128);
}

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -156,7 +152,7 @@ private function displayJson(OutputInterface $output, $filesInfo)
}
});

$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
$output->writeln(json_encode($filesInfo, defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0));

return min($errors, 1);
}
Expand Down
Expand Up @@ -11,10 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Console\Descriptor;

if (!defined('JSON_PRETTY_PRINT')) {
define('JSON_PRETTY_PRINT', 128);
}

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -174,7 +170,13 @@ protected function describeContainerParameter($parameter, array $options = array
*/
private function writeData(array $data, array $options)
{
$this->write(json_encode($data, (isset($options['json_encoding']) ? $options['json_encoding'] : 0) | JSON_PRETTY_PRINT)."\n");
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;

if (defined('JSON_PRETTY_PRINT')) {
$flags |= JSON_PRETTY_PRINT;
}

$this->write(json_encode($data, $flags)."\n");
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Translation/Dumper/JsonFileDumper.php
Expand Up @@ -13,10 +13,6 @@

use Symfony\Component\Translation\MessageCatalogue;

if (!defined('JSON_PRETTY_PRINT')) {
define('JSON_PRETTY_PRINT', 128);
}

/**
* JsonFileDumper generates an json formatted string representation of a message catalogue.
*
Expand All @@ -29,7 +25,7 @@ class JsonFileDumper extends FileDumper
*/
public function format(MessageCatalogue $messages, $domain = 'messages')
{
return json_encode($messages->all($domain), JSON_PRETTY_PRINT);
return json_encode($messages->all($domain), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0);
}

/**
Expand Down

0 comments on commit 851c7b8

Please sign in to comment.