Symfony internals [english]

  • 4,571 views
Uploaded on

Slides from the 'Symfony internals' talk at DeSymfony

Slides from the 'Symfony internals' talk at DeSymfony

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,571
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
36
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Who am I? Raúl Fraile @raulfrailePHP/Symfony2 freelance developer
  • 2. What do I do?
  • 3. SensioLabsConnect
  • 4. Why?
  • 5. app[_dev].phpFront controller
  • 6. 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();
  • 7. bootstrap.php.cacheSeveral classes/interfaces in the same file
  • 8. bootstrap.php.cache// app/bootstrap.php.cachenamespace { require_once __DIR__./autoload.php; }namespace SymfonyComponentDependencyInjection{ interface ContainerAwareInterface { function setContainer(ContainerInterface $container = null); } ...}
  • 9. bootstrap.php.cacheReduces I/O operations
  • 10. bootstrap.php.cacheLoads autoload.php
  • 11. 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,));
  • 12. ClassLoader Autoloading ofclasses/interfaces in Symfony2
  • 13. ClassLoader
  • 14. ClassLoaderImplements PSR-0
  • 15. PSR-0 Approved by the “Framework Interop Group”github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
  • 16. PSR-0Fully Qualified Name SymfonyCoreRequest ZendMailMessage [vendor_path]/Symfony/Core/Request.php [vendor_path]/Zend/Mail/Message.php Filesystem
  • 17. 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; } }}
  • 18. ClassLoader $loader->findFile( SymfonyComponentHttpFoundationRequest );/Sites/desymfony/app/../vendor/symfony/src/ Symfony/Component/HttpFoundation/Request.php
  • 19. 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();
  • 20. 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); }}
  • 21. 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
  • 22. AppKernel.phpIf (true === $debug) { Saves initial time (microtime) display_errors = 1 error_reporting = -1 DebugUniversalClassLoader} else { display_errors = 0}
  • 23. 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();
  • 24. LoadClassCache Objective: map FQN/filenames ofclasses and interfaces
  • 25. LoadClassCache It is cached inclasses.map andclasses.php.meta
  • 26. 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();
  • 27. RequestHttpFoundation Component
  • 28. RequestOO abstraction of a HTTP request
  • 29. 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 ...
  • 30. 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();
  • 31. HttpKernel Wrapper on top ofRequest/Response tohandle the dynamic part of HTTP
  • 32. HttpKernel Handles anenvironment consisting of bundles, DIC...
  • 33. $kernel->boot() Initializebundles and DIC
  • 34. $kernel->initializeBundles()Loads bundles defined inAppKernel::registerBundles()
  • 35. $kernel->initializeContainer() Generated using the ContainerBuilder from DependencyInjection
  • 36. 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
  • 37. $kernel->initializeContainer() {rootDir}{Environment} [Debug]ProjectContainer
  • 38. $kernel->initializeContainer() For each bundle,Bundle::build() method is executed and extensions are loaded
  • 39. $kernel->boot() For each bundle, thecontainer is injected and the boot() method is executed
  • 40. $kernel->handle()Goal: Return a Response object
  • 41. Event kernel.requestDispatched as soon as the request arrives
  • 42. Event kernel.requestAny listener can returna Response and end the execution
  • 43. Event kernel.request Used byFrameworkBundle to set the _controller value
  • 44. RouterListenerUses a RouterMatcher(autogenerated by theRouting component)
  • 45. 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)); } ... }
  • 46. ControllerResolverMust return the controller + arguments from _controller
  • 47. FrameworkBundle Ties components andlibraries together to make a MVC framework
  • 48. FrameworkBundleMoreover...
  • 49. FrameworkBundlephp app/console
  • 50. 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();
  • 51. FrameworkBundleCommands assets:install cache:clear cache:warmup container:debugrouter:dump-apache router:debug
  • 52. FrameworkBundleBase Controller
  • 53. FrameworkBundle// src/Acme/DemoBundle/Controller/DemoControllernamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;class DemoController extends Controller{ public function helloAction($name) { ... }}
  • 54. FrameworkBundleAnd much more: ESI, WebTestCase, DataCollectors...
  • 55. Event kernel.controllerOnce the controller has been resolved, this event is generated
  • 56. Event kernel.controller Any listener can manipulate theController callable
  • 57. Event kernel.viewThis event is called only if the Controller doesnot return a Response
  • 58. Event kernel.viewGoal: build a Responseobject from the returnvalue of the Controller
  • 59. Event kernel.response Allows to modify orreplace the Responseobject after its creation
  • 60. Event kernel.exceptionLast change to convert a Exception into a Response object
  • 61. EventsExtend from KernelEventgetRequestType(): MASTER_REQUEST or SUB_REQUESTgetKernel();getRequest();
  • 62. 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
  • 63. 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();
  • 64. Response::send()Send headers and content
  • 65. Response::send()
  • 66. Demohttps://github.com/raulfraile/ internals-desymfony2012
  • 67. ¡Thank you!
  • 68. Photoshttp://www.flickr.com/photos/connectirmeli/7233514862 http://www.flickr.com/photos/barretthall/6070677596http://www.flickr.com/photos/f-oxymoron/5005673112/
  • 69. Questions?