Skip to content

Commit

Permalink
Merge pull request #44 from pug-php/symfony-5
Browse files Browse the repository at this point in the history
Allow Symfony 5
  • Loading branch information
kylekatarnls authored Jan 14, 2020
2 parents 1de7d58 + 03420ca commit e1d4738
Show file tree
Hide file tree
Showing 51 changed files with 1,158 additions and 233 deletions.
9 changes: 5 additions & 4 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
version: "2"
exclude_patterns:
- "tests/"
- "templates/"
- "genTravis.php"
plugins:
duplication:
enabled: true
exclude_patterns:
- "tests/"
- "genTravis.php"
config:
languages:
- php
- php
fixme:
enabled: true
phpmd:
Expand Down
17 changes: 16 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,29 @@ matrix:
env:
- SYMFONY_VERSION='4.3.*'
- PUG_VERSION='^3.0.0'
- php: 7.4snapshot
- php: 7.4
env:
- SYMFONY_VERSION='4.4.*'
- PUG_VERSION='^3.0.0'
- php: 7.4
env:
- STABILITY='dev'
- REMOVE_DEV_PACKAGE="codeclimate/php-test-reporter"
- SYMFONY_VERSION='5.0.*'
- TWIG_VERSION='^2.10.0'
- PUG_VERSION='^3.0.0'
- php: 7.4
env:
- STABILITY='dev'
- REMOVE_DEV_PACKAGE="codeclimate/php-test-reporter"
- SYMFONY_VERSION='5.0.*'
- TWIG_VERSION='^3.0.0'
- PUG_VERSION='^3.0.0'
before_script:
- if [ "$REMOVE_PACKAGE" != "" ]; then travis_retry composer remove --no-update -n ${REMOVE_PACKAGE}; fi;
- if [ "$REMOVE_DEV_PACKAGE" != "" ]; then travis_retry composer remove --dev --no-update -n ${REMOVE_DEV_PACKAGE}; fi;
- if [ "$SYMFONY_VERSION" != "" ]; then travis_retry composer require --no-update -n symfony/symfony=$SYMFONY_VERSION; fi;
- if [ "$TWIG_VERSION" != "" ]; then travis_retry composer require --no-update -n twig/twig=$TWIG_VERSION; fi;
- if [ "$SYMFONY_VERSION" = "3.0.*" ]; then travis_retry composer require --no-update -n twig/twig=^1; fi;
- if [ "$PUG_VERSION" != "" ]; then travis_retry composer require --no-update -n pug-php/pug=$PUG_VERSION; fi;
- if [ "$STABILITY" != "" ]; then travis_retry composer config minimum-stability ${STABILITY}; fi;
Expand Down
66 changes: 66 additions & 0 deletions cache-templates/PugDebugTemplateTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Twig\Environment;
use Twig\Source;
use Twig\Template;

/* {{filename}} */
class PugDebugTemplateTemplate extends Template
{
private $source;
private $macros = [];

public function __construct(Environment $env)
{
parent::__construct($env);
$this->source = $this->getSourceContext();
$this->parent = false;
$this->blocks = [];
}

protected function doDisplay(array $context, array $blocks = [])
{
$macros = $this->macros;
extract($context);

if (isset($this->extensions["Symfony\\Bundle\\WebProfilerBundle\\Twig\\WebProfilerExtension"])) {
$__internal_1 = $this->extensions["Symfony\\Bundle\\WebProfilerBundle\\Twig\\WebProfilerExtension"];
$__internal_1->enter($__internal_1_prof = new \Twig\Profiler\Profile($this->getTemplateName(), "template", "{{filename}}"));
}

if (isset($this->extensions["Symfony\\Bridge\\Twig\\Extension\\ProfilerExtension"])) {
$__internal_2 = $this->extensions["Symfony\\Bridge\\Twig\\Extension\\ProfilerExtension"];
$__internal_2->enter($__internal_2_prof = new \Twig\Profiler\Profile($this->getTemplateName(), "template", "{{filename}}"));
}

// {{code}}

if (isset($__internal_1)) {
$__internal_1->leave($__internal_1_prof);
}

if (isset($__internal_2)) {
$__internal_2->leave($__internal_2_prof);
}
}

public function getTemplateName()
{
return "{{filename}}";
}

public function isTraitable()
{
return false;
}

public function getDebugInfo()
{
return [/* {{debugInfo}} */];
}

public function getSourceContext()
{
return new Source("{{source}}", "{{filename}}", "{{path}}");
}
}
47 changes: 47 additions & 0 deletions cache-templates/PugTemplateTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Twig\Environment;
use Twig\Source;
use Twig\Template;

/* {{filename}} */
class PugTemplateTemplate extends Template
{
private $source;
private $macros = [];

public function __construct(Environment $env)
{
parent::__construct($env);
$this->source = $this->getSourceContext();
$this->parent = false;
$this->blocks = [];
}

protected function doDisplay(array $context, array $blocks = [])
{
$macros = $this->macros;
extract($context);
// {{code}}
}

public function getTemplateName()
{
return "{{filename}}";
}

public function isTraitable()
{
return false;
}

public function getDebugInfo()
{
return [/* {{debugInfo}} */];
}

public function getSourceContext()
{
return new Source("", "{{filename}}", "{{path}}");
}
}
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
"pug/installer": "^0.1.0",
"pug-php/pug": "^2.7.1 || ^3.0.0",
"pug-php/pug-assets": "^1.0.1",
"symfony/framework-bundle": "^2.7 || ^3.0 || ^4.0",
"symfony/http-foundation": "^2.7 || ^3.0 || ^4.0",
"symfony/http-kernel": "^2.7 || ^3.0 || ^4.0",
"symfony/security-bundle": "^2.7 || ^3.0 || ^4.0",
"symfony/templating": "^2.7 || ^3.0 || ^4.0",
"symfony/twig-bridge": "^2.7 || ^3.0 || ^4.0"
"symfony/framework-bundle": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/http-foundation": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/http-kernel": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/security-bundle": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/templating": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"symfony/twig-bridge": "^2.7 || ^3.0 || ^4.0 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.7 || ~6.3.0",
"symfony/phpunit-bridge": "^3.3.9, !=3.4.31",
"symfony/symfony": "^2.7 || ^3.0 || ^4.0",
"symfony/symfony": "^2.7 || ^3.0 || ^4.0 || ^5.0",
"codeclimate/php-test-reporter": "dev-master",
"composer/composer": ">=1.0.0"
},
Expand Down
19 changes: 19 additions & 0 deletions polyfill/Pug/Twig/EnvironmentTwig1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Pug\Twig;

/**
* @codeCoverageIgnore
*/
class EnvironmentTwig1 extends EnvironmentBase
{
public function compileSource($source, $name = null)
{
return $this->compileSourceBase($source);
}

public function render($name, array $context = [])
{
return $this->renderBase($name, array_merge($this->pugSymfonyEngine->getSharedVariables(), $context));
}
}
21 changes: 21 additions & 0 deletions polyfill/Pug/Twig/EnvironmentTwig2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Pug\Twig;

use Twig\Source;

/**
* @codeCoverageIgnore
*/
class EnvironmentTwig2 extends EnvironmentBase
{
public function compileSource(Source $source)
{
return $this->compileSourceBase($source);
}

public function render($name, array $context = [])
{
return $this->renderBase($name, array_merge($this->pugSymfonyEngine->getSharedVariables(), $context));
}
}
38 changes: 38 additions & 0 deletions polyfill/Pug/Twig/EnvironmentTwig3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Pug\Twig;

use Twig\Source;
use Twig\Template;

/**
* @codeCoverageIgnore
*/
class EnvironmentTwig3 extends EnvironmentBase
{
/**
* @var string[]
*/
protected $classNames = [];

public function compileSource(Source $source): string
{
return $this->compileSourceBase($source);
}

public function loadTemplate(string $cls, string $name, int $index = null): Template
{
if ($index !== null) {
$cls .= '___' . $index;
}

$this->classNames[$name] = $cls;

return parent::loadTemplate($cls, $name, $index);
}

public function render($name, array $context = []): string
{
return $this->renderBase($name, array_merge($this->pugSymfonyEngine->getSharedVariables(), $context));
}
}
14 changes: 14 additions & 0 deletions src/Jade/Exceptions/ReservedVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Jade\Exceptions;

use RuntimeException;
use Throwable;

class ReservedVariable extends RuntimeException
{
public function __construct($variableName, $code = 0, Throwable $previous = null)
{
parent::__construct("$variableName is a reserved variable name, you can't overwrite it.", $code, $previous);
}
}
15 changes: 12 additions & 3 deletions src/Jade/JadeSymfonyBundle/Command/AssetsPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

if (!class_exists('Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand')) {
// @codeCoverageIgnoreStart
class_alias('Jade\\JadeSymfonyBundle\\Command\\PugAwareCommand', 'Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand');
// @codeCoverageIgnoreEnd
}

class AssetsPublishCommand extends ContainerAwareCommand
{
protected function configure()
Expand All @@ -27,6 +33,7 @@ protected function cacheTemplates($pug)
$errors = 0;
$errorDetails = [];
$directories = [];

foreach ($pug->getOption('viewDirectories') as $viewDirectory) {
if (is_dir($viewDirectory) && !in_array($viewDirectory, $directories)) {
$directories[] = $viewDirectory;
Expand All @@ -42,17 +49,19 @@ protected function cacheTemplates($pug)

protected function execute(InputInterface $input, OutputInterface $output)
{
list($directories, $success, $errors, $errorDetails) = $this->cacheTemplates(
$this->getContainer()->get('templating.engine.pug')->getEngine()
);
$symfonyEngine = isset($this->pugSymfonyEngine) ? $this->pugSymfonyEngine : $this->getContainer()->get('templating.engine.pug');
list($directories, $success, $errors, $errorDetails) = $this->cacheTemplates($symfonyEngine->getEngine());
$count = count($directories);
$output->writeln($count . ' ' . ($count === 1 ? 'directory' : 'directories') . ' scanned: ' . implode(', ', $directories) . '.');
$output->writeln($success . ' templates cached.');
$output->writeln($errors . ' templates failed to be cached.');

foreach ($errorDetails as $index => $detail) {
$output->writeln("\n" . ($index + 1) . ') ' . $detail['inputFile']);
$output->writeln($detail['error']->getMessage());
$output->writeln($detail['error']->getTraceAsString());
}

return 0;
}
}
17 changes: 17 additions & 0 deletions src/Jade/JadeSymfonyBundle/Command/PugAwareCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Jade\JadeSymfonyBundle\Command;

use Pug\PugSymfonyEngine;
use Symfony\Component\Console\Command\Command;

abstract class PugAwareCommand extends Command
{
protected $pugSymfonyEngine;

public function __construct(PugSymfonyEngine $pugSymfonyEngine)
{
$this->pugSymfonyEngine = $pugSymfonyEngine;
parent::__construct(null);
}
}
33 changes: 33 additions & 0 deletions src/Jade/JadeSymfonyBundle/JadeSymfonyBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,41 @@

namespace Jade\JadeSymfonyBundle;

use Jade\Symfony\Traits\PrivatePropertyAccessor;
use Pug\PugSymfonyBundle\Command\AssetsPublishCommand;
use Pug\PugSymfonyEngine;
use ReflectionMethod;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class JadeSymfonyBundle extends Bundle
{
use PrivatePropertyAccessor;

protected $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;

if ($container) {
$engine = new PugSymfonyEngine($container->get('kernel'));
$services = static::getPrivateProperty($container, 'services', $propertyAccessor);
$services['Pug\\PugSymfonyEngine'] = $engine;
$propertyAccessor->setValue($container, $services);
}
}

public function registerCommands(Application $application)
{
$method = new ReflectionMethod('Pug\\PugSymfonyBundle\\Command\\AssetsPublishCommand', '__construct');
$class = $method->getNumberOfParameters() === 1 ? $method->getParameters()[0]->getClass() : null;

if ($class && $class->getName() === 'Pug\\PugSymfonyEngine') {
$application->addCommands([
new AssetsPublishCommand($this->container->get('Pug\\PugSymfonyEngine')),
]);
}
}
}
Loading

0 comments on commit e1d4738

Please sign in to comment.