Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Last active November 15, 2018 12:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jverdeyen/cbcee38229d53545ca4789543c36aba3 to your computer and use it in GitHub Desktop.
Save jverdeyen/cbcee38229d53545ca4789543c36aba3 to your computer and use it in GitHub Desktop.
Symfony Components in a Legacy application - http://tech.yappa.be/symfony-components-in-a-legacy-php-application
#!/usr/bin/env php
<?php
include_once 'app/global_vars.php';
require ROOT_DIR . 'vendor/autoload.php';
use Application\Command\InsertFixturesCommand;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new InsertFixturesCommand);
$application->run();
<?php
include_once 'app/global_vars.php';
require ROOT_DIR . 'vendor/autoload.php';
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(ROOT_DIR . 'app/config/'));
$loader->load('services.yml');
$loader->load('parameters.yml');
$container->setParameter('root_dir', ROOT_DIR);
$container->setParameter('env', ENVIRONMENT);
$container->compile();
// boot legacy application logic
$legacyFrontController = new \Application\Controller\LegacyFrontController($container);
echo $legacyFrontController->start();
{
"require": {
"php": "^5.6",
"doctrine/annotations": "^1.2",
"doctrine/cache": "^1.6",
"doctrine/common": "^2.5",
"doctrine/dbal": "^2.5",
"doctrine/orm": "^2.5",
"symfony/config": "^3.1",
"symfony/console": "^3.1",
"symfony/dependency-injection": "^3.1",
"symfony/expression-language": "^3.1",
"symfony/finder": "^3.1",
"symfony/form": "^3.1",
"symfony/http-foundation": "^3.1",
"symfony/http-kernel": "^3.1",
"symfony/routing": "^3.1",
"symfony/security-csrf": "^3.1",
"symfony/twig-bridge": "^3.1",
"symfony/validator": "^3.1",
"symfony/var-dumper": "^3.1",
"symfony/yaml": "^3.1",
"twig/twig": "~2.0@dev"
}
}
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\Routing\Matcher\UrlMatcher;
/** @var Request $request */
$request = Request::createFromGlobals();
/** @var UrlMatcher $urlMatcher */
$urlMatcher = $this->container->get('route.url_matcher');
/** @var ControllerResolver $controllerResolver */
$controllerResolver = $this->container->get('route.controller_resolver');
$request->attributes->add($urlMatcher->match($request->getPathInfo()));
$controller = $controllerResolver->getController($request);
<?php
use Symfony\Component\Form\Form;
/** @var Form $form */
$form = $this->container->get('form.factory')->getFormFactory()->create(CustomType::class, $modelClass);
index:
path: /
defaults: { _controller: 'Application\Controller\HomeController::indexAction'}
profile_index:
path: /profile
defaults: { _controller: 'Application\Controller\ProfileController::indexAction' }
services:
request:
class: Symfony\Component\HttpFoundation\Request
factory: [Symfony\Component\HttpFoundation\Request, createFromGlobals]
request.context:
class: Symfony\Component\Routing\RequestContext
calls:
- ["fromRequest", ["@request"]]
route.file_locator:
class: Symfony\Component\Config\FileLocator
arguments:
- ["%root_dir%/app/config"]
route.yaml_file_loader:
class: Symfony\Component\Routing\Loader\YamlFileLoader
arguments:
- "@route.file_locator"
router:
class: Symfony\Component\Routing\Router
arguments:
- "@route.yaml_file_loader"
- "routing.yml"
- []
- null
- null
route.url_matcher:
class: Symfony\Component\Routing\Matcher\UrlMatcher
arguments:
- "@=service('router').getRouteCollection()"
- "@request.context"
route.controller_resolver:
class: Symfony\Component\HttpKernel\Controller\ControllerResolver
twig.loader:
class: Twig_Loader_Filesystem
calls:
- ["addPath", ["%root_dir%/src/Resources/views/"]]
- ["addPath", ["%root_dir%/vendor/symfony/twig-bridge/Resources/views/"]]
twig:
class: Twig_Environment
arguments:
- "@twig.loader"
-
cache: "%cache_dir%/Twig"
debug: true
calls:
- ["addExtension", ["@twig.translation_extension"]]
- ["addExtension", ["@twig.route_extension"]]
- ["addExtension", ["@twig.form_extension"]]
twig.translation_extension:
class: Symfony\Bridge\Twig\Extension\TranslationExtension
arguments:
- "@translator"
twig.form_engine:
class: Symfony\Bridge\Twig\Form\TwigRendererEngine
arguments: [["Form/form_div_layout.html.twig"]]
calls:
- ["setEnvironment", ["@twig"]]
twig.form_renderer:
class: Symfony\Bridge\Twig\Form\TwigRenderer
arguments: ["@twig.form_engine"]
twig.route_extension:
class: Symfony\Bridge\Twig\Extension\RoutingExtension
arguments:
- "@router"
twig.form_extension:
class: Symfony\Bridge\Twig\Extension\FormExtension
arguments: ["@twig.form_renderer"]
csrf.token_generator:
class: Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator
csrf.token_storage:
class: Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage
csrf.manager:
class: Symfony\Component\Security\Csrf\CsrfTokenManager
arguments:
- "@csrf.token_generator"
- "@csrf.token_storage"
form.csrf_extension:
class: Symfony\Component\Form\Extension\Csrf\CsrfExtension
arguments:
- "@csrf.manager"
- "@translator"
translator.xliff_file_loader:
class: Symfony\Component\Translation\Loader\XliffFileLoader
translator:
class: Symfony\Component\Translation\Translator
arguments:
- "nl_NL"
calls:
- ["addLoader", ["xlf", "@translator.xliff_file_loader"]]
- ["addResources", ["xlf", "%root_dir%/vendor/symfony/validator/Symfony/Component/Validator/Resources/translations/validators.nl.xlf", "nl_NL", "messages"]]
- ["setFallbackLocales", ["nl"]]
validator:
class: Symfony\Component\Validator\ValidatorBuilder
factory: [Symfony\Component\Validator\Validation, createValidatorBuilder]
calls:
- ["enableAnnotationMapping"]
form.http_extension:
class: Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension
form.validator_extension:
class: Symfony\Component\Form\Extension\Validator\ValidatorExtension
arguments:
- "@=service('validator').getValidator()"
form.factory:
class: Symfony\Component\Form\FormFactoryBuilder
factory: [Symfony\Component\Form\Forms, createFormFactoryBuilder]
calls:
- ["addExtension", ["@form.http_extension"]]
- ["addExtension", ["@form.csrf_extension"]]
- ["addExtension", ["@form.validator_extension"]]
<?php
...
dump($variable);
...
@piotrkochan
Copy link

Don't do that:

        arguments:
            - "@=service('router').getRouteCollection()"
            - "@request.context"

It will make Symfony initialization time very long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment