Symfoy2源碼分析——啟動過程1,symfoy2源碼_PHP教程

來源:互聯網
上載者:User

Symfoy2源碼分析——啟動過程1,symfoy2源碼


    本文通過閱讀分析Symfony2的源碼,瞭解Symfony2啟動過程中完成哪些工作,從閱讀源碼瞭解Symfony2架構。

    Symfony2的核心本質是把Request轉換成Response的一個過程。

    我們大概看看入口檔案(web_dev.php)的源碼,入口檔案從總體上描述了Symfony2架構的工作的流程:

 1 require_once __DIR__.'/../app/AppKernel.php'; 2  3 $kernel = new AppKernel('dev', true); 4 $kernel->loadClassCache(); 5 //利用請求資訊($_GET $_POST $_SERVER等等)構造Request對象 6 $request = Request::createFromGlobals(); 7 //Symfony2架構核心工作就是把Request對象轉換成Response對象 8 $response = $kernel->handle($request); 9 //向用戶端輸出Response對象10 $response->send();11 //完成一些耗時的後台操作,例如郵件發送,圖片裁剪等等耗時工作12 $kernel->terminate($request, $response);

    Symfony2架構通過用戶端的請求資訊來決定產生並返迴響應的資料,我們下面的Symfony2源碼分析重點就是AppKernel::handle方法。

    AppKernel::handle的實現繼承於Kernel::handle

 1     /** 2      * 3      * @param Request $request Request對象執行個體 4      * @param int     $type    請求的類型(子請求 or 主請求) 5      * @param bool    $catch   是否捕捉異常 6      * 7      * @return Response Response對象執行個體 8      * 9      */    10     public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)11     {   12         //$this->booted Symfony2架構只啟動一次13         if (false === $this->booted) {14             //初始化並啟動所有註冊在AppKernel裡面的所有bundles(AppKernel::registerBundles)15             //初始化container16             //載入、緩衝配置資料和路由資料、編譯container容器等,為後面事件處理做準備。17             $this->boot();18         }19 20         //開啟事件處理,Symfony2核心的請求處理過程本質是一系列的事件處理過程21         return $this->getHttpKernel()->handle($request, $type, $catch);22     }

    AppKernel::boot方法

 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         //初始化註冊到AppKernel裡的所有bundle(AppKernel::registerBundles)13         $this->initializeBundles();14 15         // init container16         //初始化並編譯緩衝container,包括載入配置資訊、編譯資訊、service等17         //Symfony2的核心組件的載入,和各個組件之間的關聯關係都在container容器初始化中完成,所以這會是下面詳細描述18         $this->initializeContainer();19 20         //把bundle注入到container,並啟動bundle21         foreach ($this->getBundles() as $bundle) {22             $bundle->setContainer($this->container);23             $bundle->boot();24         }25 26         //標記Symfony2隻啟動一次並啟動成功27         $this->booted = true;28     }

    AppKernel::initializeContainer源碼解析

 1     protected function initializeContainer() 2     { 3         //檢查app/cache/dev[prod]快取檔案是否到期,以container快取檔案的最後修改時間為參考時間, 4         //如果app/cache/dev[prod]下的存在一個或者多個快取檔案的最後修改時間大於container快取檔案的 5         //最後修改時間,就判斷為緩衝到期。 6         //另外,如果$this->debug為false(即關閉debug的情況下)只要container快取檔案存在,那麼就認為 7         //緩衝不到期 8         $class = $this->getContainerClass(); 9         $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);10         $fresh = true;11         if (!$cache->isFresh()) {12             //初始化一個ContainerBuilder對象執行個體;13             //自動載入所有註冊的Bundle的DependencyInjection下的所有extension,Bundle可以通過extension來載入屬於該Bundle配置(service的配置、14             //路由的配置等等)、Bundle的全域變數等15             //同時這些extension載入的資訊都會被儲存到container中;16             //載入並儲存compiler pass到container,為下一步compile做準備,我們可以通過compiler pass修改已經註冊到container的service的屬性17             //compiler pass的官方文檔http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html18             $container = $this->buildContainer();19             //執行compiler pass 的process方法,container的compile過程主要是執行上一步儲存到container內的compiler pass 的process方法20             $container->compile();21             //產生container的緩衝(appDevDebugProjectContainer.php),該container包含了service的擷取方法、別名的映射關係22             $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());23 24             $fresh = false;25         }26 27         28         require_once $cache;29 30         $this->container = 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             //載入DependencyInjection下的Extension,所有Extension必需實現Extension介面 6             if ($extension = $bundle->getContainerExtension()) { 7                 $container->registerExtension($extension); 8                 $extensions[] = $extension->getAlias(); 9             }10 11             //開啟debug的情況下,把bundles添加到recourses12             if ($this->debug) {13                 $container->addObjectResource($bundle);14             }15         }16         foreach ($this->bundles as $bundle) {17             //通常用來添加compiler pass18             $bundle->build($container);19         }20 21         // ensure these extensions are implicitly loaded22         $container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));23     }

    從AppKernel::initializeContainer可以看出Bundle和container是Symfony2架構的基礎核心,container是Symfony2架構的所有組件的統一管理中心,Bundle就是一個功能模組的組織。

    如果你好奇service、配置參數是怎樣被載入的,可以詳細去瞭解Symfony2的Extension;如果你好奇怎麼對已經載入了的service進一步完善和修改,可有詳細瞭解Symfony2的compiler pass。

    到了這一步,Symfony2架構啟動幾乎完成,為後面的核心事件處理EventDispatcher::dispatch做好了準備。

    下一篇講解Symfony2架構的核心事件處理。


易語言怎寫一個源碼,使得可以通過控制視窗1內的按鈕,來控制啟動視窗的屬性變換,比如換膚,



這是用按鈕點擊事件進行變換皮膚,如果放在啟動視窗下面回出現每次開啟皮膚會不一樣的。


 

誰可以給我一個怎使我編的軟體隨機啟動的易語言源碼?

用下面這個代碼就可以了,很簡單的,

寫註冊項(3,“software\microsoft\windows\CurrentVersion\Run\我的啟動項”,“你程式所在的目錄+檔案名稱”)
 

http://www.bkjia.com/PHPjc/894763.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/894763.htmlTechArticleSymfoy2源碼分析——啟動過程1,symfoy2源碼 本文通過閱讀分析Symfony2的源碼,瞭解Symfony2啟動過程中完成哪些工作,從閱讀源碼瞭解Symfony2架構...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.