Skip to content

Commit

Permalink
Merge pull request #96 from vitorbrandao/GH-95-replace-templating-cla…
Browse files Browse the repository at this point in the history
…sses-deprecated-in-symfony-4

GH-95 replace templating classes deprecated in Symfony 4
  • Loading branch information
vitorbrandao authored Nov 19, 2021
2 parents dbb1b96 + bde053e commit 206befc
Show file tree
Hide file tree
Showing 99 changed files with 2,692 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.phpunit.cache
.php-cs-fixer.cache
composer.lock
/cache
/docs/_build/
/tests/Sandbox/App/cache
/tests/Sandbox/App/logs
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude(['tests/Sandbox', 'tools', 'tmp'])
->exclude(['cache', 'tests/Sandbox', 'tools', 'tmp'])
->in(__DIR__)
;

Expand Down
26 changes: 15 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,27 @@ build: ## Build Docker images (in parallel)
$(MAKE) -j4 build-php7-sf4 build-php7-sf5 build-php8-sf4 build-php8-sf5
.PHONY: build

test-php7-sf4: ## Run unit and functional tests in the php7-sf4 image
test-php7-sf4: ## Run phpunit tests in the php7-sf4 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf4 composer test
.PHONY: test-php7-sf4

test-php7-sf5: ## Run unit and functional tests in the php7-sf5 image
test-php7-sf5: ## Run phpunit tests in the php7-sf5 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php7-sf5 composer test
.PHONY: test-php7-sf5

test-php8-sf4: ## Run unit and functional tests in the php8-sf4 image
test-php8-sf4: ## Run phpunit tests in the php8-sf4 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf4 composer test
.PHONY: test-php8-sf4

test-php8-sf5: ## Run unit and functional tests in the php8-sf5 image
test-php8-sf5: ## Run phpunit tests in the php8-sf5 image
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor noiselabs/smarty-bundle-testing:php8-sf5 composer test
.PHONY: test-php8-sf5

test: ## Run unit and functional tests
test: ## Run phpunit tests
$(MAKE) test-php7-sf4 test-php7-sf5 test-php8-sf4 test-php8-sf5
.PHONY: test

test-parallel: ## Run unit and functional tests in parallel
test-parallel: ## Run phpunit tests in parallel
$(MAKE) -j4 test-php7-sf4 test-php7-sf5 test-php8-sf4 test-php8-sf5
.PHONY: test-parallel

Expand All @@ -153,19 +153,23 @@ sh-php8-sf5: ## Get a shell in the php8-sf5 container
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php8-sf5 sh
.PHONY: sh-php8-sf5

web-php7-sf4: ## Launches the built-in PHP web server in the php7-sf4 container
web-php7-sf4: ## Launches the built-in PHP web server in the php7-sf4 container
@echo http://localhost:8074/
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -p 127.0.0.1:8074:8080/tcp -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php7-sf4 php -S 0.0.0.0:8080 -t web
.PHONY: web-php7-sf4

web-php7-sf5: ## Launches the built-in PHP web server in the php7-sf5 container
web-php7-sf5: ## Launches the built-in PHP web server in the php7-sf5 container
@echo http://localhost:8075/
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -p 127.0.0.1:8075:8080/tcp -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php7-sf5 php -S 0.0.0.0:8080 -t web
.PHONY: web-php7-sf5

web-php8-sf4: ## Launches the built-in PHP web server in the php8-sf4 container
web-php8-sf4: ## Launches the built-in PHP web server in the php8-sf4 container
@echo http://localhost:8084/
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -p 127.0.0.1:8084:8080/tcp -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php8-sf4 php -S 0.0.0.0:8080 -t web
.PHONY: web-php8-sf4

web-php8-sf5: ## Launches the built-in PHP web server in the php8-sf5 container
web-php8-sf5: ## Launches the built-in PHP web server in the php8-sf5 container
@echo http://localhost:8085/
docker run --rm -it --mount type=bind,src=$(PWD),dst=/app --mount type=volume,dst=/app/vendor -p 127.0.0.1:8085:8080/tcp -w /app/tests/Sandbox/App noiselabs/smarty-bundle-testing:php8-sf5 php -S 0.0.0.0:8080 -t web
.PHONY: web-php8-sf5

Expand Down Expand Up @@ -193,5 +197,5 @@ composer-sf5: ## Install composer dependencies using sf5

### PHPUnit tests ###
phpunit: ## Run PHPUnit tests
XDEBUG_MODE=coverage phpunit --coverage-text --coverage-html=.phpunit.cache/html
XDEBUG_MODE=coverage phpunit --debug --coverage-text --coverage-html=.phpunit.cache/html
.PHONY: phpunit
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
"ext-simplexml": "*",
"smarty/smarty": "^3.1.40",
"symfony/framework-bundle": "^4|^5",
"symfony/polyfill-php80": "^1.23",
"symfony/templating": "^4|^5"
},
"require-dev": {
"phpunit/phpunit": "*",
"symfony/asset": "^4|^5",
"symfony/browser-kit": "^4|^5",
"symfony/console": "^4|^5",
"symfony/css-selector": "^4|^5",
"symfony/expression-language": "^4|^5",
"symfony/phpunit-bridge": "^5",
"symfony/security": "^3.4.48|^4|^5",
Expand Down
30 changes: 2 additions & 28 deletions config/smarty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
<argument type="service" id="smarty.templating.loader"/>
<argument type="service" id="service_container" />
<argument>%smarty.options%</argument>
<argument type="service" id="templating.globals" on-invalid="null" />
<argument type="service" id="smarty.templating.globals" on-invalid="null" />
<argument type="service" id="logger" on-invalid="null" />
<tag name="templating.engine" />
<tag name="monolog.logger" channel="SmartyBundle" />
</service>

<service id="smarty.templating.loader" class="NoiseLabs\Bundle\SmartyBundle\Loader\TemplateLoader">
<argument type="service" id="templating.name_parser"/>
<argument type="service" id="smarty.templating.loader.filesystem" />
</service>

<service id="smarty.file_locator" class="Symfony\Component\HttpKernel\Config\FileLocator">
<factory class="NoiseLabs\Bundle\SmartyBundle\Loader\FileLocatorFactory" method="createFileLocator"/>
<argument type="service" id="kernel" />
Expand All @@ -33,27 +28,13 @@
<argument>%smarty.options.templates_dir%</argument>
</service>

<service id="smarty.templating.locator" class="Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator">
<argument type="service" id="smarty.file_locator" />
<argument>%kernel.cache_dir%</argument>
</service>

<service id="smarty.templating.loader.filesystem" class="Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader">
<argument type="service" id="smarty.templating.locator" />
</service>

<service id="smarty.extension.core" class="NoiseLabs\Bundle\SmartyBundle\Extension\CoreExtension" public="false">
<tag name="smarty.extension" />
</service>

<service id="smarty.extension.actions" class="NoiseLabs\Bundle\SmartyBundle\Extension\ActionsExtension" public="false">
<tag name="smarty.extension" />
<argument type="service" id="templating.helper.actions" />
</service>

<service id="templating.helper.actions" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper">
<tag name="templating.helper" alias="actions" />
<argument type="service" id="fragment.handler" />
<argument type="service" id="smarty.templating.helper.actions" />
</service>

<service id="smarty.extension.assets" class="NoiseLabs\Bundle\SmartyBundle\Extension\AssetsExtension" public="false">
Expand Down Expand Up @@ -82,12 +63,5 @@
</service>

<service id="smarty_engine" alias="templating.engine.smarty" />

<service id="smarty.templating.finder" class="NoiseLabs\Bundle\SmartyBundle\Loader\TemplateFinder" public="true">
<argument type="service" id="kernel" />
<argument type="service" id="templating.filename_parser" />
<argument>%kernel.root_dir%/Resources</argument>
<argument>%smarty.options%</argument>
</service>
</services>
</container>
46 changes: 46 additions & 0 deletions config/templating.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults public="false" />

<service id="smarty.templating.name_parser" class="NoiseLabs\Bundle\SmartyBundle\Templating\TemplateNameParser">
<argument type="service" id="kernel" />
</service>

<service id="smarty.templating.filename_parser" class="NoiseLabs\Bundle\SmartyBundle\Templating\TemplateFilenameParser" />

<service id="smarty.templating.locator" class="NoiseLabs\Bundle\SmartyBundle\Templating\Loader\TemplateLocator">
<argument type="service" id="smarty.file_locator" />
<argument>%kernel.cache_dir%</argument>
</service>

<service id="smarty.templating.finder" class="NoiseLabs\Bundle\SmartyBundle\Loader\TemplateFinder" public="true">
<argument type="service" id="kernel" />
<argument type="service" id="smarty.templating.filename_parser" />
<argument>%kernel.root_dir%/Resources</argument>
<argument>%smarty.options%</argument>
</service>

<service id="smarty.templating.loader.filesystem" class="NoiseLabs\Bundle\SmartyBundle\Templating\Loader\FilesystemLoader">
<argument type="service" id="smarty.templating.locator" />
</service>

<service id="smarty.templating.loader" class="NoiseLabs\Bundle\SmartyBundle\Loader\TemplateLoader">
<argument type="service" id="smarty.templating.name_parser"/>
<argument type="service" id="smarty.templating.loader.filesystem" />
</service>

<service id="smarty.templating.globals" class="NoiseLabs\Bundle\SmartyBundle\Templating\GlobalVariables">
<argument type="service" id="service_container" />
</service>

<service id="smarty.templating.helper.actions" class="NoiseLabs\Bundle\SmartyBundle\Templating\Helper\ActionsHelper">
<tag name="templating.helper" alias="actions" />
<argument type="service" id="fragment.handler" />
</service>
</services>
</container>
9 changes: 6 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<php>
<ini name="assert.exception" value="1"/>
<ini name="date.timezone" value="Europe/London"/>
<ini name="error_reporting" value="22519"/>
<ini name="error_reporting" value="-1"/>
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="log_errors_max_len" value="0"/>
Expand All @@ -33,8 +33,11 @@
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="functional">
<directory suffix="Test.php">tests/Functional</directory>
<testsuite name="integration">
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
<testsuite name="application">
<directory suffix="Test.php">tests/Application</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 2 additions & 0 deletions src/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->write(sprintf("- Failed to compile <error>%d</error> files.\n", $count['failed']));
}
$output->write(sprintf("- Total compilation time: <comment>%f secs</comment>.\n", $totalCtime));

return $count['failed'] > 0 ? -1 : 0;
}
}
2 changes: 2 additions & 0 deletions src/Command/TemplatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($templates as $path => $template) {
$io->writeln(sprintf('* <comment>%s</comment>', $path));
}

return 0;
}
}
35 changes: 27 additions & 8 deletions src/DependencyInjection/SmartyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace NoiseLabs\Bundle\SmartyBundle\DependencyInjection;

use Exception;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand All @@ -46,10 +47,33 @@ class SmartyExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('smarty.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$this->registerSmartyConfiguration($config, $container, $loader);
$this->registerTemplatingConfiguration($config, $container, $loader);

if ($this->hasConsole()) {
// Console commands
$loader->load('console.xml');
}
}

public function getAlias()
{
return 'smarty';
}

protected function hasConsole(): bool
{
return class_exists(Application::class);
}

private function registerSmartyConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
$loader->load('smarty.xml');

$engineDefinition = $container->getDefinition('templating.engine.smarty');

if (!empty($config['globals'])) {
Expand All @@ -76,15 +100,10 @@ public function load(array $configs, ContainerBuilder $container)
foreach ($config['options'] as $k => $v) {
$container->setParameter('smarty.options.'.$k, $v);
}

// Console commands
if (class_exists('\\Symfony\\Component\\Console\\Application')) {
$loader->load('console.xml');
}
}

public function getAlias()
private function registerTemplatingConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
return 'smarty';
$loader->load('templating.xml');
}
}
39 changes: 14 additions & 25 deletions src/Extension/ActionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\BlockPlugin;
use NoiseLabs\Bundle\SmartyBundle\Extension\Plugin\ModifierPlugin;
use NoiseLabs\Bundle\SmartyBundle\Templating\Helper\ActionsHelper;
use SmartyException;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;

/**
* SmartyBundle extension for Symfony actions helper.
Expand Down Expand Up @@ -65,16 +65,18 @@ public function getPlugins()
/**
* Returns the Response content for a given controller or URI.
*
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
* @param null|mixed $template
* @param null|mixed $repeat
* @param null|string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
* @param null|mixed $template
* @param null|mixed $repeat
*
* @see Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper::render()
* @see Symfony\Bundle\TwigBundle\Extension\ActionsExtension::renderAction()
* @throws SmartyException
*
* @return mixed
*
* @see \Symfony\Bundle\TwigBundle\Extension\ActionsExtension::renderAction()
* @see \NoiseLabs\Bundle\SmartyBundle\Templating\Helper\ActionsHelper::render()
*/
public function renderBlockAction(array $parameters = [], $controller = null, $template = null, &$repeat = null)
public function renderBlockAction(array $parameters = [], $controller = null, $template = null, &$repeat = null): string
{
// only output on the closing tag
if (!$repeat) {
Expand All @@ -96,7 +98,7 @@ public function renderBlockAction(array $parameters = [], $controller = null, $t
*
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
*
* @see \Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper::render()
* @see \NoiseLabs\Bundle\SmartyBundle\Templating\Helper\ActionsHelper::render()
* @see \Symfony\Bundle\TwigBundle\Extension\ActionsExtension::renderAction()
*/
public function renderModifierAction($controller, array $attributes = [], array $options = [])
Expand All @@ -106,12 +108,8 @@ public function renderModifierAction($controller, array $attributes = [], array

/**
* @param $controller
*
* @return mixed
*
* @since Symfony-2.2
*/
protected function render($controller, array $attributes = [], array $options = [])
protected function render($controller, array $attributes = [], array $options = []): string
{
$renderOptions = [];
if (isset($options['standalone']) && true === $options['standalone']) {
Expand All @@ -125,19 +123,10 @@ protected function render($controller, array $attributes = [], array $options =
}

$isControllerReference = false !== strpos($controller, ':');
$uri = $isControllerReference ? $this->actionsHelper->controller($controller, $attributes, $options) : $controller;
$options = $isControllerReference ? $renderOptions : array_merge($renderOptions, $attributes);

return $this->getActionsHelper()->render(
$isControllerReference ? $this->getActionsHelper()->controller($controller, $attributes, $options) : $controller,
$isControllerReference ? $renderOptions : array_merge($renderOptions, $attributes)
);
}

/**
* @return ActionsHelper
*/
protected function getActionsHelper()
{
return $this->actionsHelper;
return $this->actionsHelper->render($uri, $options);
}

/**
Expand Down
Loading

0 comments on commit 206befc

Please sign in to comment.