Last active
February 3, 2016 13:59
-
-
Save jenkoian/6e00d8a8569ce4afa99f to your computer and use it in GitHub Desktop.
Legacy escape route AppKernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
use application\App; | |
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | |
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | |
use Symfony\Bundle\SecurityBundle\SecurityBundle; | |
use Symfony\Bundle\TwigBundle\TwigBundle; | |
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Definition; | |
use Symfony\Component\DependencyInjection\Reference; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Kernel; | |
use Symfony\Component\Routing\RouteCollectionBuilder; | |
class AppKernel extends Kernel | |
{ | |
use MicroKernelTrait; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function registerBundles() | |
{ | |
return [ | |
new FrameworkBundle(), | |
new TwigBundle(), | |
new SensioFrameworkExtraBundle(), | |
new SecurityBundle() | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) | |
{ | |
// Order here is important | |
$this->configureParameters($c); | |
$this->configureFramework($c); | |
$this->configureTwig($c); | |
$this->configureServices($c); | |
$this->configureSecurity($c); | |
$this->configureTwigExtensions($c); | |
$this->configureListeners($c); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configureRoutes(RouteCollectionBuilder $routes) | |
{ | |
$routes->add($prefix . '/example', 'kernel:exampleAction'); | |
$routes->import(__DIR__ . '/Framework/Controller/'); | |
} | |
/** | |
* @param Request $request | |
* @return Response | |
*/ | |
public function exampleAction(Request $request) | |
{ | |
/** @var \Twig_Environment $twig */ | |
$twig = $this->getContainer()->get('twig'); | |
$content = $twig->render('example.html.twig'); | |
return new Response($content); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCacheDir() | |
{ | |
// Use cache dir from env var if applicable | |
if (false !== $cacheDir = getenv('CACHE_DIR')) { | |
return $cacheDir . DIRECTORY_SEPARATOR . $this->getEnvironment(); | |
} | |
// Use cache dir from legacy config if applicable | |
if (false !== $cacheDir = App::getConfig()->paths['temp']['cache']) { | |
return $cacheDir . DIRECTORY_SEPARATOR . $this->getEnvironment(); | |
} | |
// Finally fall back to default cache dir | |
return parent::getCacheDir(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getLogDir() | |
{ | |
// Use log dir from env var if applicable | |
if (false !== $logDir = getenv('LOG_DIR')) { | |
return $logDir . DIRECTORY_SEPARATOR . $this->getEnvironment(); | |
} | |
// Use log dir from legacy config if applicable | |
if (false !== $logDir = App::getConfig()->paths['temp']['logs']) { | |
return $logDir . DIRECTORY_SEPARATOR . $this->getEnvironment(); | |
} | |
// Finally fall back to default log dir | |
return parent::getLogDir(); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureParameters(ContainerBuilder $c) | |
{ | |
// Map global legacy config object to parameters on the container | |
$config = App::getConfig(); | |
$c->setParameter('legacy.paths', $config->paths); | |
$c->setParameter('legacy.analytics', $config->analytics); | |
$c->setParameter('legacy.debug', $config->debug); | |
$c->setParameter('legacy.comments', $config->comments); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureTwigExtensions(ContainerBuilder $c) | |
{ | |
$c->register('app.language_extension', 'Acme\Twig\LanguageExtension') | |
->addArgument(new Reference('app.language')) | |
->setPublic(false) | |
->addTag('twig.extension'); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureListeners(ContainerBuilder $c) | |
{ | |
$c->register('app.language_listener', 'Acme\Listener\LanguageListener') | |
->addArgument(new Reference('app.language')) | |
->addArgument(new Reference('app.language_session')) | |
->addTag('kernel.event_listener', array('event' => 'kernel.request')); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureSecurity(ContainerBuilder $c) | |
{ | |
$c->setDefinition( | |
'app.legacy_authenticator', | |
new Definition( | |
'Acme\Framework\Security\LegacyAuthenticator', | |
[ | |
new Reference('app.authentication_session'), | |
new Reference('app.legacy_user_manager') | |
] | |
) | |
); | |
$c->setDefinition( | |
'app.legacy_user_provider', | |
new Definition('Acme\Framework\Security\LegacyUserProvider') | |
); | |
$c->loadFromExtension( | |
'security', | |
[ | |
'providers' => [ | |
'legacy' => [ | |
'id' => 'app.legacy_user_provider' | |
] | |
], | |
'encoders' => [ | |
'Acme\\Authentication\\User' => 'plaintext' | |
], | |
'firewalls' => [ | |
'main' => [ | |
'pattern' => '^/', | |
'anonymous' => true, | |
'logout' => true, | |
'guard' => [ | |
'authenticators' => [ | |
'app.legacy_authenticator' | |
], | |
], | |
], | |
], | |
'access_control' => [ | |
[ | |
'path' => '^/secret/content/only/for/moderators', | |
'role' => 'ROLE_MODERATOR' | |
], | |
] | |
] | |
); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureTwig(ContainerBuilder $c) | |
{ | |
$c->loadFromExtension( | |
'twig', | |
[ | |
'paths' => [ | |
__DIR__ . '/../views/', | |
], | |
'globals' => [ | |
'google_analytics_code' => $c->getParameter('legacy.analytics')['google'], | |
] | |
] | |
); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureFramework(ContainerBuilder $c) | |
{ | |
$c->loadFromExtension( | |
'framework', | |
[ | |
'secret' => '__SECRET__', | |
'default_locale' => 'eng', | |
'templating' => [ | |
'engines' => ['twig'] | |
], | |
'session' => [ | |
'storage_id' => 'session.storage.php_bridge' | |
] | |
] | |
); | |
} | |
/** | |
* @param ContainerBuilder $c | |
*/ | |
protected function configureServices(ContainerBuilder $c) | |
{ | |
$c->register('legacy.user_manager', 'model\user\User'); | |
$c->register('legacy.avatar', 'classes\avatar\Avatar'); | |
$c->register('app.legacy_avatar', 'Acme\Avatar\LegacyAvatarAdapter') | |
->addArgument(new Reference('legacy.avatar')); | |
$c->register('app.language', 'Acme\Language\LegacyLanguageAdapter'); | |
$c->register('app.language_session', 'Acme\Language\LegacyLanguageSessionAdapter'); | |
$c->register('app.legacy_user_manager', 'Acme\Authentication\LegacyUserManagerAdapter') | |
->addArgument(new Reference('legacy.user_manager')) | |
->addArgument(new Reference('app.legacy_avatar')); | |
$c->register('app.authentication', 'Acme\Authentication\LegacyAuthenticationAdapter'); | |
$c->register('app.authentication_session', 'Acme\Authentication\LegacyAuthenticationSessionAdapter'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment