Skip to content

[TwigBundle] Deprecate the public "twig" service to private #36739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Mime

* Deprecated `Address::fromString()`, use `Address::create()` instead

TwigBundle
----------

* Deprecated the public `twig` service to private.

Validator
---------

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ Security
* Removed `DefaultLogoutSuccessHandler` in favor of `DefaultLogoutListener`.
* Added a `logout(Request $request, Response $response, TokenInterface $token)` method to the `RememberMeServicesInterface`.

TwigBundle
----------

* The `twig` service is now private.

Validator
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testBundlePublicDir()
public function testBundleTwigTemplatesDir()
{
static::bootKernel(['test_case' => 'BundlePaths']);
$twig = static::$container->get('twig');
$twig = static::$container->get('twig.alias');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we get private services in tests with the special test container? why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The twig service is still public. We don't want to access to it directly to avoid the deprecation.

$bundlesMetadata = static::$container->getParameter('kernel.bundles_metadata');

$this->assertSame([$bundlesMetadata['LegacyBundle']['path'].'/Resources/views'], $twig->getLoader()->getPaths('Legacy'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ framework:

twig:
strict_variables: '%kernel.debug%'

services:
twig.alias:
alias: twig
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Environment;

class LoginController implements ContainerAwareInterface
class LoginController implements ServiceSubscriberInterface
{
use ContainerAwareTrait;
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function loginAction()
{
Expand All @@ -43,4 +50,15 @@ public function secureAction()
{
throw new \Exception('Wrapper', 0, new \Exception('Another Wrapper', 0, new AccessDeniedException()));
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return [
'form.factory' => FormFactoryInterface::class,
'twig' => Environment::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Environment;

class LocalizedController implements ContainerAwareInterface
class LocalizedController implements ServiceSubscriberInterface
{
use ContainerAwareTrait;
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function loginAction(Request $request)
{
Expand Down Expand Up @@ -61,4 +67,14 @@ public function homepageAction()
{
return (new Response('<html><body>Homepage</body></html>'))->setPublic();
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return [
'twig' => Environment::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Environment;

class LoginController implements ContainerAwareInterface
class LoginController implements ServiceSubscriberInterface
{
use ContainerAwareTrait;
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not inject dependencies explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to modify existing tests the least possible. I mean I tried to keep $container->get. I guess we can inject the dependencies as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the service subscriber system looks OK to me 🤷‍♂️

}

public function loginAction(Request $request, UserInterface $user = null)
{
Expand Down Expand Up @@ -53,4 +59,14 @@ public function secureAction()
{
throw new \Exception('Wrapper', 0, new \Exception('Another Wrapper', 0, new AccessDeniedException()));
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return [
'twig' => Environment::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle;

use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller\LocalizedController;
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller\LoginController;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class FormLoginBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test bundle is used with different configurations, it's easier to register those controllers as services here (it avoids duplication in config files).

{
parent::build($container);

$container
->register(LoginController::class)
->setPublic(true)
->addTag('container.service_subscriber');

$container
->register(LocalizedController::class)
->setPublic(true)
->addTag('container.service_subscriber');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ services:
tags:
- { name: form.type }

Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Controller\LoginController:
public: true
tags:
- { name: container.service_subscriber }

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----

* deprecated the public `twig` service to private

5.0.0
-----

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
->tag('container.preload', ['class' => ExtensionSet::class])
->tag('container.preload', ['class' => Template::class])
->tag('container.preload', ['class' => TemplateWrapper::class])
->tag('container.private', ['package' => 'symfony/twig-bundle', 'version' => '5.2'])

->alias('Twig_Environment', 'twig')
->alias(Environment::class, 'twig')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;

Expand All @@ -26,7 +27,7 @@ public function test()
$kernel->boot();

$container = $kernel->getContainer();
$content = $container->get('twig')->render('index.html.twig');
$content = $container->get('twig.alias')->render('index.html.twig');
$this->assertStringContainsString('{ a: b }', $content);
}

Expand Down Expand Up @@ -60,7 +61,7 @@ public function registerBundles(): iterable

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function ($container) {
$loader->load(function (ContainerBuilder $container) {
$container
->loadFromExtension('framework', [
'secret' => '$ecret',
Expand All @@ -69,6 +70,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
->loadFromExtension('twig', [
'default_path' => __DIR__.'/templates',
])
->setAlias('twig.alias', 'twig')->setPublic(true)
;
});
}
Expand Down