Magento INIT process bare Essen
Posted by Branko in magento | comments off
This
Article is meant to be a start up point for "newbies" getting ready
Digg seriously into the magetno. When I say newbie's, I meant no
Disrespect, I only ment new to magento. Because, you cannot be newbie
PHP developer and do something usefull with magento since it requires
Extensive knowledge of OOP and MVC.
Step 1: init/run the magento
File:
/Index. php
Code (from line 52 ):
Mage::run();
Eplanation:
MageIs the Class Name of/APP/MAGE. phpFile. MAGE class is of Type final, meaning you cannot extend it. In the code above, we are calling the static methodRun ()OnMageClass.
Step 2: Overview of static run () method
File:
/APP/MAGE. php
Code (from line 447 ):
public static function run($code = '', $type = 'store', $options=array()){ try { Varien_Profiler::start('mage'); Varien_Profiler::start('mage::app'); self::app($code, $type, $options); Varien_Profiler::stop('mage::app'); Varien_Profiler::start('mage::dispatch'); self::app()->getFrontController()->dispatch(); Varien_Profiler::stop('mage::dispatch'); Varien_Profiler::stop('mage'); } ... /* exception handling code here, irrelevant for this example */}
Explanation:
InsideRun ()MethodMageClass two important things happen at the grand scale of things. Feel free to ignoreVarien_profiler: Start ()AndVarien_profiler: Stop ()
At this point since they are irrelevant for this base introductory.
Those two important things I mentioned are two method callinsied this
Run () method, and those are:
- SELF: APP ($ code, $ type, $ options);/* On Line 453 */
- SELF: APP ()-> getfrontcontroller ()-> dispatch ();/* On Line 457 */
Both of these method callpoint us to the same base method, so let's disect that in next step.
Step 3: overview of self: APP () method
File:
/APP/MAGE. php
Code (from line 416 ):
public static function app($code = '', $type = 'store', $options=array()){ if (null === self::$_app) { Varien_Profiler::start('mage::app::construct'); self::$_app = new Mage_Core_Model_App(); Varien_Profiler::stop('mage::app::construct'); Mage::setRoot(); Mage::register('events', new Varien_Event_Collection()); Varien_Profiler::start('mage::app::register_config'); Mage::register('config', new Mage_Core_Model_Config()); Varien_Profiler::stop('mage::app::register_config'); Varien_Profiler::start('mage::app::init'); self::$_app->init($code, $type, $options); Varien_Profiler::stop('mage::app::init'); self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS); } return self::$_app;}
Explanation:
InsideAPP ()Method We have several essenthings happening. Try to ignoreVarien_profiler: Start ()AndVarien_profiler: Stop ()Stuff. Now let's review the code. First we haveSELF: $ _ APP = new mage_core_model_app ()Called, saying store me and instaceMage_core_model_appInto my local (from the point of viewMageClass)$ _ AppVariable.
Note the definitionAPP ()Method, it'sAPP ($ code = ", $ type = 'store', $ Options = array ()).
It parameters es three main parameters (or none, in which case it uses
Defaults). Anyhow, this method call sets up entire object of TypeMage_core_model_appAnd stores it inMageClass local variable$ _ App.
Then we have three method CILS, one after other setting up pageMageClass Object:
- Mage: setroot ();
- Mage: Register ('events', new varien_event_collection ());
- Mage: Register ('config', new mage_core_model_config ());
After which we are pointed back to ourMage_core_model_appObject instance by following method cballs:
- SELF: $ _ app-> Init ($ code, $ type, $ options );
- SELF: $ _ app-> loadareapart (mage_core_model_app_area: area_global, mage_core_model_app_area: part_events );
Note thatSELF: $ _ appIs storing the instaceMage_core_model_appSo it's perfectly OK to call any public method thatMage_core_model_appClass has on this variable.
I will not go into the details of what both of those method CILS
Do, since this article wocould take hours and hours to be written in such
A way to cover all the inner workings details.
For now, let's just remember the return typeAPP ()Method call, returnSELF: $ _ app, It's the instanceMage_core_model_appClass. In short,APP ()Method sets up and returns entire app model.
Now, let's have a look back to step 2. There is one more method call left to be covered, we'll do that in Step 4
Step 4: Overview of self: APP ()-> getfrontcontroller ()-> dispatch ()
File:
/APP/code/CORE/MAGE/CORE/model/APP. php
Code (from line 857 ):
public function getFrontController(){ if (!$this->_frontController) { $this->_initFrontController(); } return $this->_frontController;}
Explanation:
Mage_core_model_appClass is all about setting currently running application instance options. method callGetfrontcontroller ()Returns the result of the privateMage_core_model_appMethod called_ Initfrontcontroller ().
While_ Initfrontcontroller ()CILS and sets$ This-> _ frontcontroller = new mage_core_controller_varien_front (), Where $ this variable is in contextMage_core_model_appClass, it returns$ This, Or in short, it returns current instaceMage_core_model_appClass.
SinceSELF: APP ()-> getfrontcontroller ()Method call returned us and object instance of TypeMage_core_model_appWe are allowed to chain a methodDispatch ()To it. Since this is the last method call insideRun ()Method ofMageClass file, this makes it a full circle.
Keep in mind that this is really, really, reallyMost basic intro
To magento's INIT process. There are a ton of stuff that happen
Each method call in this process and I only covered the most basic one.
For instance, when you calledSELF: APP ()You "triggered chained" cballs of your methods that might call other classes and Methods essential to build a instanceMage_core_model_app. Not to mention the inheritance and so on.
I find it a penpencil and a piece of paper to be a good tools
Tracking and "Drawing" yourself magento application inner workings.
Core things are really massive, a lot of file are involved into
Magento INIT process and lover core inner workings. It gets easy
Loose track of things jumping from one class to another.