• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Symfony internals [english]
 

Symfony internals [english]

on

  • 4,388 views

Slides from the 'Symfony internals' talk at DeSymfony

Slides from the 'Symfony internals' talk at DeSymfony

Statistics

Views

Total Views
4,388
Views on SlideShare
4,291
Embed Views
97

Actions

Likes
3
Downloads
28
Comments
0

6 Embeds 97

https://twitter.com 63
http://www.symfony.es 27
http://lanyrd.com 3
http://www.linkedin.com 2
http://www.sfexception.com 1
http://dev.symfony2developer.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Symfony internals [english] Symfony internals [english] Presentation Transcript

    • Who am I? Raúl Fraile @raulfrailePHP/Symfony2 freelance developer
    • What do I do?
    • SensioLabsConnect
    • Why?
    • app[_dev].phpFront controller
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • bootstrap.php.cacheSeveral classes/interfaces in the same file
    • bootstrap.php.cache// app/bootstrap.php.cachenamespace { require_once __DIR__./autoload.php; }namespace SymfonyComponentDependencyInjection{ interface ContainerAwareInterface { function setContainer(ContainerInterface $container = null); } ...}
    • bootstrap.php.cacheReduces I/O operations
    • bootstrap.php.cacheLoads autoload.php
    • autoload.php// app/autoload.phpuse SymfonyComponentClassLoaderUniversalClassLoader;$loader = new UniversalClassLoader();$loader->registerNamespaces(array( Symfony => array( __DIR__./../vendor/symfony/src, __DIR__./../vendor/bundles), Assetic => __DIR__./../vendor/assetic/src,));$loader->registerPrefixes(array( Twig_ => __DIR__./../vendor/twig/lib,));
    • ClassLoader Autoloading ofclasses/interfaces in Symfony2
    • ClassLoader
    • ClassLoaderImplements PSR-0
    • PSR-0 Approved by the “Framework Interop Group”github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
    • PSR-0Fully Qualified Name SymfonyCoreRequest ZendMailMessage [vendor_path]/Symfony/Core/Request.php [vendor_path]/Zend/Mail/Message.php Filesystem
    • ClassLoader// namespaced class name$namespace = substr($class, 0, $pos);foreach ($this->namespaces as $ns => $dirs) { if (0 !== strpos($namespace, $ns)) { continue; } foreach ($dirs as $dir) { $className = substr($class, $pos + 1); $file = $dir . DIR_SEPARATOR . str_replace(,DIR_SEPARATOR, $namespace) . DIR_SEPARATOR . str_replace(_, DIR_SEPARATOR, $className) . .php; if (file_exists($file)) { return $file; } }}
    • ClassLoader $loader->findFile( SymfonyComponentHttpFoundationRequest );/Sites/desymfony/app/../vendor/symfony/src/ Symfony/Component/HttpFoundation/Request.php
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • AppKernel.php// src/AppKernel.phpuse SymfonyComponentHttpKernelKernel;use SymfonyComponentConfigLoaderLoaderInterface;class AppKernel extends Kernel{ public function registerBundles() { $bundles = array( new SymfonyBundleTwigBundleTwigBundle(), ... ); return $bundles; } public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load(__DIR__./config/config_.$this->getEnvironment()..yml); }}
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send(); Debug Environment
    • AppKernel.phpIf (true === $debug) { Saves initial time (microtime) display_errors = 1 error_reporting = -1 DebugUniversalClassLoader} else { display_errors = 0}
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • LoadClassCache Objective: map FQN/filenames ofclasses and interfaces
    • LoadClassCache It is cached inclasses.map andclasses.php.meta
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • RequestHttpFoundation Component
    • RequestOO abstraction of a HTTP request
    • Request RequestGET /index.php HTTP/1.1␍␊Host: test.com␍␊ $_GET queryAccept-Language:en;q=0.8␍␊ requestAccept-Encoding:gzip␍␊ $_POST cookiesUser-Agent: Mozilla/5.0␍␊␍␊ files $_COOKIE server headers $_FILES getHost $_SERVER getClientIp ...
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • HttpKernel Wrapper on top ofRequest/Response tohandle the dynamic part of HTTP
    • HttpKernel Handles anenvironment consisting of bundles, DIC...
    • $kernel->boot() Initializebundles and DIC
    • $kernel->initializeBundles()Loads bundles defined inAppKernel::registerBundles()
    • $kernel->initializeContainer() Generated using the ContainerBuilder from DependencyInjection
    • ContainerBuilder// example.com/src/container.phpuse SymfonyComponentDependencyInjection;use SymfonyComponentDependencyInjectionReference;$sc = new DependencyInjectionContainerBuilder();$sc->register(context, SymfonyComponentRoutingRequestContext);$sc->register(matcher, SymfonyComponentRoutingMatcherUrlMatcher) ->setArguments(array($routes, new Reference(context)));$sc->register(framework, SimplexFramework) ->setArguments(array(new Reference(dispatcher), newReference(resolver))) ; http://fabien.potencier.org/article/62/create-your-own-framework-on-top-of-the-symfony2-components-part-12
    • $kernel->initializeContainer() {rootDir}{Environment} [Debug]ProjectContainer
    • $kernel->initializeContainer() For each bundle,Bundle::build() method is executed and extensions are loaded
    • $kernel->boot() For each bundle, thecontainer is injected and the boot() method is executed
    • $kernel->handle()Goal: Return a Response object
    • Event kernel.requestDispatched as soon as the request arrives
    • Event kernel.requestAny listener can returna Response and end the execution
    • Event kernel.request Used byFrameworkBundle to set the _controller value
    • RouterListenerUses a RouterMatcher(autogenerated by theRouting component)
    • RouterListener// app/cache/dev/appdevUrlMatcher.phpclass appdevUrlMatcher extends RedirectableUrlMatcher{ ... public function match($pathinfo) { ... // _demo_hello if (0 === strpos($pathinfo, /demo/hello) && preg_match(#^/demo/hello/(?P<name>[^/]+?)$#s, $pathinfo, $m)) { return array_merge($this->mergeDefaults($m, array( _controller => AcmeDemoBundleControllerDemoController::helloAction) ), array( _route => _demo_hello)); } ... }
    • ControllerResolverMust return the controller + arguments from _controller
    • FrameworkBundle Ties components andlibraries together to make a MVC framework
    • FrameworkBundleMoreover...
    • FrameworkBundlephp app/console
    • FrameworkBundle// app/console#!/usr/bin/env php<?phprequire_once __DIR__./bootstrap.php.cache;require_once __DIR__./AppKernel.php;use SymfonyBundleFrameworkBundleConsoleApplication;use SymfonyComponentConsoleInputArgvInput;$input = new ArgvInput();$env = $input->getParameterOption(array(--env, -e), getenv(SYMFONY_ENV) ?: dev);$debug = !$input->hasParameterOption(array(--no-debug, ));$kernel = new AppKernel($env, $debug);$application = new Application($kernel);$application->run();
    • FrameworkBundleCommands assets:install cache:clear cache:warmup container:debugrouter:dump-apache router:debug
    • FrameworkBundleBase Controller
    • FrameworkBundle// src/Acme/DemoBundle/Controller/DemoControllernamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;class DemoController extends Controller{ public function helloAction($name) { ... }}
    • FrameworkBundleAnd much more: ESI, WebTestCase, DataCollectors...
    • Event kernel.controllerOnce the controller has been resolved, this event is generated
    • Event kernel.controller Any listener can manipulate theController callable
    • Event kernel.viewThis event is called only if the Controller doesnot return a Response
    • Event kernel.viewGoal: build a Responseobject from the returnvalue of the Controller
    • Event kernel.response Allows to modify orreplace the Responseobject after its creation
    • Event kernel.exceptionLast change to convert a Exception into a Response object
    • EventsExtend from KernelEventgetRequestType(): MASTER_REQUEST or SUB_REQUESTgetKernel();getRequest();
    • ResponseResponse HTTP/1.1 200 OK Headers Content-type: text/html Date:Thu, 31 May 2012 17:54:50 GMT Version Content <!DOCTYPE HTML> <html lang="es"> <head>Status code <meta charset="utf-8"> ...Status text Charset
    • app[_dev].php// web/app[_env].php1. require_once __DIR__./../app/bootstrap.php.cache;2. require_once __DIR__./../app/AppKernel.php;3. use SymfonyComponentHttpFoundationRequest;4. $kernel = new AppKernel(dev, true);5. $kernel->loadClassCache();6. $kernel->handle(Request::createFromGlobals())->send();
    • Response::send()Send headers and content
    • Response::send()
    • Demohttps://github.com/raulfraile/ internals-desymfony2012
    • ¡Thank you!
    • Photoshttp://www.flickr.com/photos/connectirmeli/7233514862 http://www.flickr.com/photos/barretthall/6070677596http://www.flickr.com/photos/f-oxymoron/5005673112/
    • Questions?