- 自由主题
- app.yml 与 setting.yml 的差别是什么
- 基本都是截取原文,但我把思路和文中阐述的层次关系缕顺出来了
-
Bootstrap
启动流程
-
web/frontend_dev.php
这是 前台的 controller
- 一共两行代码
-
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
- allowing you to pass
a custom root directory for your application
as the fourth argument of
ProjectConfiguration::getApplicationConfiguration()
-
sfContext::createInstance($configuration)->dispatch();
- a custom context class as the third (and last)
argument of sfContext::createInstance()
- but remember it has to extend sfContext
-
sfProjectConfiguration
基类
-
ProjectConfiguration
- 继承 sfProjectConfiguarationi
-
This is fortunate
since much of the
project is
configured inside the
sfProjectConfiguration
constructor
- several useful values are computed and stored,
such as the project’s root directory and the symfony
library directory.
- sfProjectConfiguration also creates a new event
dispatcher of type sfEventDispatcher
- 当然,除非在 front controller 里预先设置好
ProjectConfiguration::getApplicationConfiguration()
的第五个参数
- 比如 enable 和
disable 各个 plugin
- by using sfProjectConfiguration::setPlugins()
- 接下来 plugins
are loaded by
sfProjectConfiguration
::loadPlugins()
- the developer has a chance
to interact with this process through
the sfProjectConfiguration::setupPlugins()
that can be overriden
- Plugin initialization is quite straight forward. For each plugin, symfony looks for a
${plugin}Configuration (e.g. sfGuardPluginConfiguration) class and instantiates it
if found. Otherwise, sfPluginConfigurationGeneric is used.
- You can hook into a plugin’s
configuration through two methods
- ${plugin}Configuration::configure(), before autoloading is done
- ${plugin}Configuration::initialize(), after autoloading
- sfSocialPlugin 中的 listener,在这里hook 一下,以便监听
- 接下来, sfApplicationConfiguration 执行插件的 configure()
- 这可以在sfApplicationConfiguration::initConfiguration()
之前更改application’s configuration
- 这里有好几个入口点
可以挂钩 hook
- For example, you can interact with the autoloader’s configuration by
connecting to the autoload.filter_config event
- Next,several very important configuration files are loaded,
including settings.yml and app.yml
- Finally, a last bit of plugin configuration is available through
each plugin’s config/config.php file or configuration class’s initialize() method
- If sf_check_lock
is activated
- symfony will now check for a lock file (the one created by the
project:disable task, for example
- f the lock is found, the following
files are checked and the first
available is included, followed
immediately by termination of the script
- apps/${application}/config/unavailable.php
- config/unavailable.php
- web/errors/unavailable.php
- lib/vendor/symfony/lib/exception/data/unavailable.php
- Finally, the developer has one last chance to customize the application’s initialization through
the sfApplicationConfiguration::initialize() method
-
每个app
- 每个 app 都有自己的 configuaration
- apps/${application}/config/${application}Configuration.class.php
- sfApplicationConfiguration actually extends ProjectConfiguration
-
接着 frontendConfiguration extends sfApplicationConfiguration
- meaning that any method in ProjectConfiguration
can be shared between all applications
-
整个流程
- 1:: ProjectConfiguration::setup() (define your plugins here)
1:: Plugins are loaded
1:: ${plugin}Configuration::configure()
1:: ${plugin}Configuration::initialize()
1:: ProjectConfiguration::setupPlugins() (setup your plugins here)
1:: ${application}Configuration::configure()
1:: autoload.filter_config is notified
1:: Loading of settings.yml and app.yml
1:: ${application}Configuration::initialize()
-
sfContext
and
Factories
-
factories.yml
-
每个程序只能
有一个实例的
放在这里
- response
- mailer
- user
- routing
- view_cache_manager
- 工厂模式
- factories are a set of components or classes that your application relies on.
-
Each factory is
configured via factories.yml
- factories.yml is compiled by a config handler (more on config
handlers later) and converted into PHP
code that actually instantiates the factory objects
- you can view this code in your cache in the
cache/frontend/dev/config/config_factories.yml.php file
- Factory loading happens upon sfContext initialization.
sfContext::initialize() and sfContext::loadFactories() for information.
-
在cache/frontend/dev/config/config_factories.yml.php
里 各种factory 的初始化都是有顺序的,那是因为某些工厂会依赖其他
-
比如the routing component obviously
needs the request to
retrieve the information it needs
- 没明白,可能还是对
routing的机理不是很清楚
-
request
- By default, the sfWebRequest class
represents the request
-
Upon instantiation, sfWebRequest::initialize()81 is called,
which gathers relevant information::
- the GET / POST parameters
- the HTTP method
- You’re then given an opportunity to add your own request processing through the
request.filter_parameters event
- 《more with symfony》P131
-
routing
- You can hook up to this process through the routing.load_configuration event
- routing.load_configuration event gives you access to the current routing
object’s instance (by default, sfPatternRouting
- 类似 sfForkApplyPlugin 里做的一样
-
Once all factories have been loaded and properly setup,
the context.load_factories event is triggered
- it’s the earliest event in the framework
-
This is also the time to connect to another
very useful event: template.filter_parameters
- This event occurs whenever a
file is rendered by sfPHPView
- and allows the developer to control the
parameters actually passed to the template.
-
sfContext takes advantage of this event to
add some useful parameters to each template
- $sf_context
- $sf_request
- $sf_params
- $sf_response
- $sf_user
-
所以可以通过template.filter_parameters event 来给特定template 一个全局变量
- 例子 在书中P134
-
sfContext
- 1. Initialization of sfContext
2. Factory loading
3. Events notified:
1. request.filter_parameters
2. routing.load_configuration
3. context.load_factories
4. Global templates parameters added
-
config
handlers
- 是 symfony’s configuration system 的核心
- Each config handler is simply a class that is used to translate a set
of yaml configuration files into a block of PHP code that can be executed as needed
-
Each configuration file is assigned to one specific config handler
in the config_handlers.yml file
- http://trac.symfony-project.org/browser/branches/1.3/lib/config/
config/config_handlers.yml
- the job of a config handler is not to actually parse the yaml files (this is handled by sfYaml)
- Instead each config handler creates a set of PHP directions based on the YAML information
and saves those directions to a PHP file which can be efficiently included later
- The compiled version of each YAML configuration file can be found in the cache directory.
-
The Dispatching
andExecution
of the Request
- Once sfContext is finished initializing, the final step is to call
the controller’s dispatch() method, sfFrontWebController::dispatch()
-
In fact, sfFrontWebController::dispatch() simply
pulls the module and action names from the
request parameters and forwards the application
via sfController::forward()
- The forward method is responsible for a lot of
pre-execution checks as well as preparing the
configuration and data for the action to be executed
- parse any module name or action name from the current url, an sfError404Exception94 is raised