Heim > Artikel > Backend-Entwicklung > Quellcode-Analyse von YII (-)
$ preinit (); // Wird vorerst nicht verwendet, es wird geschätzt, dass es der späteren Erweiterung überlassen bleibt $this->initSystemHandlers();//Fehlerbehandlung festlegen $this->registerCoreComponents();//Kernkomponenten registrieren $this->configure($config); //Erweitern Sie die Eigenschaften der Klasse über die Konfigurationsdatei und tun Sie nichts, wenn sie leer ist $this->attachBehaviors($this->behaviors); $this->preloadComponents(); $this->init(); } Einige der Methoden unter $this können in der aktuellen Klasse nicht gefunden werden, da sie möglicherweise von der übergeordneten Klasse stammen. Wenn nicht, gehen Sie zur Deklaration der Klasse Überprüfen Sie: abstrakte Klasse CApplication erweitert CModule Natürlich müssen Sie CModule eingeben. Wenn Sie die gewünschte Methode immer noch nicht finden können, fahren Sie mit dem vorherigen Vorgang fort: Abstrakte Klasse CModule erweitert CComponent bis Klasse CComponent Das bedeutet, dass dies die aktuelle Heimatbasis dieser Jungs ist. Sie können es auch an den Kommentaren im Code erkennen: CComponent ist die Basisklasse für alle Komponenten Das bedeutet, dass meine Idee richtig ist, ja, es ist die Basisklasse. Durch den Code können wir feststellen, dass in unserer aktuellen Anwendung viel Logik direkt übersprungen wird, da viele Parameter leer sind. Wenn man das sieht, ist der Inhalt von $this ungefähr klar. Schauen wir noch einmal zurück neue CWebApplication($config)->run(); zurückgeben Durch die vorherige schichtweise Analyse ist die Ausführungsmethode zu diesem Zeitpunkt auch leicht zu finden. Nur in CAnwendung:
以上就介绍了YII 的源码分析(-),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> __construct(<span>$config</span>=<span>null</span><span>)
{
Yii</span>::setApplication(<span>$this</span><span>);
</span><span>//</span><span> set basePath at early as possible to avoid trouble</span>
<span>if</span>(<span>is_string</span>(<span>$config</span><span>))
</span><span>$config</span>=<span>require</span>(<span>$config</span><span>);
</span><span>if</span>(<span>isset</span>(<span>$config</span>['basePath'<span>]))
{
</span><span>$this</span>->setBasePath(<span>$config</span>['basePath'<span>]);
</span><span>unset</span>(<span>$config</span>['basePath'<span>]);
}
</span><span>else</span>
<span>$this</span>->setBasePath('protected'<span>);
Yii</span>::setPathOfAlias('application',<span>$this</span>-><span>getBasePath());
Yii</span>::setPathOfAlias('webroot',<span>dirname</span>(<span>$_SERVER</span>['SCRIPT_FILENAME'<span>]));
</span><span>if</span>(<span>isset</span>(<span>$config</span>['extensionPath'<span>]))
{
</span><span>$this</span>->setExtensionPath(<span>$config</span>['extensionPath'<span>]);
</span><span>unset</span>(<span>$config</span>['extensionPath'<span>]);
}
</span><span>else</span><span>
Yii</span>::setPathOfAlias('ext',<span>$this</span>->getBasePath().DIRECTORY_SEPARATOR.'extensions'<span>);
</span><span>if</span>(<span>isset</span>(<span>$config</span>['aliases'<span>]))
{
</span><span>$this</span>->setAliases(<span>$config</span>['aliases'<span>]);
</span><span>unset</span>(<span>$config</span>['aliases'<span>]);
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span><span> run()
{
</span><span>if</span>(<span>$this</span>->hasEventHandler('onBeginRequest'<span>)){
</span><span>$this</span>->onBeginRequest(<span>new</span> CEvent(<span>$this</span><span>));
}
</span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>,'end'),0,<span>false</span><span>);
</span><span>$this</span>-><span>processRequest();
</span><span>if</span>(<span>$this</span>->hasEventHandler('onEndRequest'<span>)){
</span><span>$this</span>->onEndRequest(<span>new</span> CEvent(<span>$this</span><span>));
}
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>abstract</span> <span>public</span> <span>function</span> processRequest(); 这个方法在当前类中是抽象的,所以肯定在它的子类中实现了。回去找CWebApplication:
<span>public</span> <span>function</span><span> processRequest()
{
</span><span>if</span>(<span>is_array</span>(<span>$this</span>->catchAllRequest) && <span>isset</span>(<span>$this</span>->catchAllRequest[0<span>]))
{
</span><span>$route</span>=<span>$this</span>->catchAllRequest[0<span>];
</span><span>foreach</span>(<span>array_splice</span>(<span>$this</span>->catchAllRequest,1) <span>as</span> <span>$name</span>=><span>$value</span><span>)
</span><span>$_GET</span>[<span>$name</span>]=<span>$value</span><span>;
}
</span><span>else</span>
<span>$route</span>=<span>$this</span>->getUrlManager()->parseUrl(<span>$this</span>-><span>getRequest());
</span><span>$this</span>->runController(<span>$route</span><span>);
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runController(<span>$route</span><span>)
{
</span><span>if</span>((<span>$ca</span>=<span>$this</span>->createController(<span>$route</span>))!==<span>null</span><span>)
{
</span><span>list</span>(<span>$controller</span>,<span>$actionID</span>)=<span>$ca</span><span>;
</span><span>$oldController</span>=<span>$this</span>-><span>_controller;
</span><span>$this</span>->_controller=<span>$controller</span><span>;
</span><span>$controller</span>-><span>init();
</span><span>$controller</span>->run(<span>$actionID</span><span>);
</span><span>$this</span>->_controller=<span>$oldController</span><span>;
}
</span><span>else</span>
<span>throw</span> <span>new</span> CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
<span>array</span>('{route}'=><span>$route</span>===''?<span>$this</span>->defaultController:<span>$route</span><span>)));
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> run(<span>$actionID</span><span>)
{
</span><span>if</span>((<span>$action</span>=<span>$this</span>->createAction(<span>$actionID</span>))!==<span>null</span><span>)
{
</span><span>if</span>((<span>$parent</span>=<span>$this</span>->getModule())===<span>null</span><span>){
</span><span>$parent</span>=Yii::<span>app();
}
</span><span>if</span>(<span>$parent</span>->beforeControllerAction(<span>$this</span>,<span>$action</span><span>))
{
</span><span>$this</span>->runActionWithFilters(<span>$action</span>,<span>$this</span>-><span>filters());
</span><span>$parent</span>->afterControllerAction(<span>$this</span>,<span>$action</span><span>);
}
}
</span><span>else</span>
<span>$this</span>->missingAction(<span>$actionID</span><span>);
}
</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runActionWithFilters(<span>$action</span>,<span>$filters</span><span>)
{
</span><span>if</span>(<span>empty</span>(<span>$filters</span><span>)){
</span><span>$this</span>->runAction(<span>$action</span><span>);
}
</span><span>else</span><span>
{
</span><span>$priorAction</span>=<span>$this</span>-><span>_action;
</span><span>$this</span>->_action=<span>$action</span><span>;
CFilterChain</span>::create(<span>$this</span>,<span>$action</span>,<span>$filters</span>)-><span>run();
</span><span>$this</span>->_action=<span>$priorAction</span><span>;
}
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:><span>public</span> <span>function</span> runAction(<span>$action</span><span>)
{
</span><span>$priorAction</span>=<span>$this</span>-><span>_action;
</span><span>$this</span>->_action=<span>$action</span><span>;
</span><span>if</span>(<span>$this</span>->beforeAction(<span>$action</span><span>))
{
</span><span>if</span>(<span>$action</span>->runWithParams(<span>$this</span>->getActionParams())===<span>false</span><span>){
</span><span>$this</span>->invalidActionParams(<span>$action</span><span>);
}
</span><span>else</span><span>{
</span><span>$this</span>->afterAction(<span>$action</span><span>);
}
}
</span><span>$this</span>->_action=<span>$priorAction</span><span>;
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:> <span>public</span> <span>function</span> runWithParams(<span>$params</span><span>)
{
</span><span>$methodName</span>='action'.<span>$this</span>-><span>getId();
</span><span>$controller</span>=<span>$this</span>-><span>getController();
</span><span>$method</span>=<span>new</span> ReflectionMethod(<span>$controller</span>, <span>$methodName</span><span>);
</span><span>if</span>(<span>$method</span>->getNumberOfParameters()>0<span>)
</span><span>return</span> <span>$this</span>->runWithParamsInternal(<span>$controller</span>, <span>$method</span>, <span>$params</span><span>);
</span><span>else</span>
<span>return</span> <span>$controller</span>-><span>$methodName</span><span>();
}</span></spanmicrosoft>
<spanmicrosoft yahei font-size:> <span>public</span> <span>function</span><span> actionIndex()
{
</span><span>echo</span> 'Hello World'<span>;
}</span></spanmicrosoft>