Symfoy2 Source Code Analysis-startup process 1, symfoy2 source code _ PHP Tutorial

Source: Internet
Author: User
Symfoy2 Source Code Analysis-start process 1, symfoy2 source code. Symfoy2 Source Code Analysis-startup process 1, symfoy2 source code this article by reading and analyzing the source code of Symfony2 to understand what work is done during Symfony2 startup, read the source code to understand the Symfony2 framework Symfoy2 Source Code Analysis-start process 1, symfoy2 source code

This article will read and analyze the source code of Symfony2 to learn about the work completed during Symfony2 startup, and learn about the Symfony2 framework from the source code.

The core of Symfony2 is the process of converting requests into Response.

Let's take a look at the source code of the entry File (web_dev.php). The entry file describes the workflow of the Symfony2 framework in general:

1 require_once _ DIR __. '/.. /app/AppKernel. php '; 2 3 $ kernel = new AppKernel ('dev', true); 4 $ kernel-> loadClassCache (); 5 // use the Request information ($ _ GET $ _ POST $ _ SERVER, etc.) to construct the request object 6 $ Request = Request: createFromGlobals (); 7 // the core work of the Symfony2 framework is to convert the Request object to the Response object 8 $ response = $ kernel-> handle ($ request ); 9 // output the Response object 10 $ response-> send (); 11 // complete some time-consuming background operations, such as sending emails, image cropping and other time-consuming work 12 $ kernel-> terminate ($ request, $ response );

The Symfony2 framework determines the data generated and returned by the client's request information. the following Symfony2 source code analysis focuses on the AppKernel: handle method.

The implementation of AppKernel: handle inherits from Kernel: handle

1/** 2*3 * @ param Request $ request Request object instance 4 * @ param int $ type Request type (subrequest or main request) 5 * @ param bool $ catch whether to catch exceptions 6*7 * @ return Response object instance 8*9 */10 public function handle (Request $ request, $ type = HttpKernelInterface :: MASTER_REQUEST, $ catch = true) 11 {12 // $ this-> booted Symfony2 framework only starts once 13 if (false = $ this-> booted) {14 // initialize and start all the bundles registered in the AppKernel (AppKernel: registerBun Dles) 15 // initialize container16 // load, cache configuration data and route data, compile the container, and prepare for subsequent event processing. 17 $ this-> boot (); 18} 19 20 // enable event processing, the request processing process of the Symfony2 kernel is essentially a series of event processing processes 21 return $ this-> getHttpKernel ()-> handle ($ request, $ type, $ catch); 22}

AppKernel: boot method

1 public function boot () 2 {3 if (true ===$ this-> booted) {4 return; 5} 6 7 if ($ this-> loadClassCache) {8 $ this-> doLoadClassCache ($ this-> loadClassCache [0], $ this-> loadClassCache [1]); 9} 10 11 // init bundles12 // Initialize all bundle registered to AppKernel (AppKernel: registerBundles) 13 $ this-> initializeBundles (); 14 15 // init container16 // initialize and compile the cache container, including loading configuration information, compilation information, service, and other 17 // loading the core components of Symfony2, the association with each component is completed in the INER container initialization, so this will be the following detailed description 18 $ this-> initializeContainer (); 19 20 // inject bundle into the container, start bundle21 foreach ($ this-> getBundles () as $ bundle) {22 $ bundle-> setContainer ($ this-> container); 23 $ bundle-> boot (); 24} 25 26 // Mark Symfony2 only once and start successfully 27 $ this-> booted = true; 28}

AppKernel: initializeContainer source code parsing

1 protected function initializeContainer () 2 {3 // check whether the app/cache/dev [prod] cached file has expired. The last modification time of the container cached file is used as the reference time, 4 // if one or more cached files exist under app/cache/dev [prod] and the last modification time is greater than 5 // The last modification time of the container cached files, the cache expires. 6 // In addition, if $ this-> debug is false (that is, when debug is disabled), as long as the container cache file exists, 7 // The cache does not expire. 8 $ class = $ this-> getContainerClass (); 9 $ cache = new ConfigCache ($ this-> getCacheDir (). '/'. $ class. '. php', $ this-> debug); 10 $ fresh = true; 11 if (! $ Cache-> isFresh () {12 // initialize a ContainerBuilder object instance; 13 // automatically load all extension under DependencyInjection of all registered Bundle, bundle can load the Bundle configuration (service configuration, 14 // route configuration, etc.) through extension) and Bundle global variables 15 // At the same time, the extension loaded information will be saved to the INER; 16 // load and save compiler pass to the container to prepare for the next compile, you can use compiler pass to modify the service attributes registered to the container. 17 // Official compiler pass documentation. http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html18 $ Container = $ this-> buildContainer (); 19 // execute the compiler pass process method, the compile process of the container is mainly to execute the process method 20 $ container-> compile (); 21 // Generate the container cache (appDevDebugProjectContainer. php). the container contains the service obtaining method and alias ing. 22 $ this-> dumpContainer ($ cache, $ container, $ class, $ this-> getContainerBaseClass (); 23 24 $ fresh = false; 25} 26 27 28 require_once $ cache; 29 30 $ this-> conta Iner = new $ class (); 31 $ this-> container-> set ('Kernel ', $ this ); 32 33 //............... 34 if (! $ Fresh & $ this-> container-> has ('cache _ warmer ') {35 $ this-> container-> get ('cache _ warmer ') -> warmUp ($ this-> container-> getParameter ('Kernel. cache_dir '); 36} 37}
1 protected function prepareContainer (ContainerBuilder $ container) 2 {3 $ extensions = array (); 4 foreach ($ this-> bundles as $ bundle) {5 // load the Extension under DependencyInjection. all Extension must implement the Extension interface 6 if ($ extension = $ bundle-> getContainerExtension ()) {7 $ container-> registerExtension ($ extension); 8 $ extensions [] = $ extension-> getAlias (); 9} 10 11 // when debug is enabled, add bundles to recourses12 if ($ this-> debug) {13 $ container-> addObjectResource ($ bundle ); 14} 15} 16 foreach ($ this-> bundles as $ bundle) {17 // usually used to add compiler pass18 $ bundle-> build ($ container ); 19} 20 21 // ensure these extensions are implicitly loaded22 $ container-> getCompilerPassConfig ()-> setMergePass (new MergeExtensionConfigurationPass ($ extensions); 23}

From AppKernel: initializeContainer, we can see that Bundle and container are the core of the Symfony2 framework. container is the central management center for all components of the Symfony2 framework, and Bundle is the organization of a functional module.

If you are curious about how the service and configuration parameters are loaded, you can learn more about the Extension of Symfony2. if you are curious about how to further improve and modify the loaded service, you can learn more about the compiler pass of Symfony2.

In this step, the Symfony2 framework is almost fully started and is ready to process EventDispatcher: dispatch for later kernel events.

The next article describes how to process kernel events in the Symfony2 framework.


How to write a source code in easy language, so that you can control the attribute changes of the startup window by controlling the buttons in window 1, such as skin replacement,



This is to change the skin by clicking the button event. if it is placed under the startup window, the skin will be different each time it is opened.



Who can give me an easy language source code for randomly starting the software I compiled?

You can use the following code, which is very simple,

Write registration items (3, "software \ microsoft \ windows \ CurrentVersion \ Run \ my startup items", "directory of your program + file name ")

In this article, we will read and analyze the source code of Symfony2 to learn about the work completed during Symfony2 startup. read the source code to understand the Symfony2 framework...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.