K4:Application Process Flow
From In-Portal Developers Guide
Current revision (01:13, 28 November 2010) (view source) (Translation) |
|||
Line 1: | Line 1: | ||
{{NotFinished}}{{toc | category = Структура приложения | sortkey = 002.001}} | {{NotFinished}}{{toc | category = Структура приложения | sortkey = 002.001}} | ||
<!-- Пишите ПОСЛЕ этой строки, пожалуйста. Ничего не стирать! --> | <!-- Пишите ПОСЛЕ этой строки, пожалуйста. Ничего не стирать! --> | ||
- | == kApplication - | + | == kApplication - main class of the system == |
- | + | The object of kApplication class is created one of the first ones and among all key properties contains links to other important classes such as Session, TemplateParser, kFactory, kHTTPQuery, kEventManager and others. At the same time, almost all other objects in the system have a back-link to the main Application object. This allows flexible interaction between different parts of the system. | |
+ | Example below, demonstrates that this kind of approach (when main application object links to other key objects) allows to call any type of event from any part of the system. | ||
<source lang="php"> | <source lang="php"> | ||
Line 12: | Line 13: | ||
</source> | </source> | ||
- | + | Method kApplication::HandleEvent passes the actual event processing to an object of kEventManager class. A link to that object is stored inside of application $this->Application->EventManager property. | |
- | + | At the same time, for event processing from kEventManager::HandleEvent method system makes calls to other methods of the main Application class. For example, in order to get the object of Event Handler for specific event we are using the following: | |
- | + | ||
- | + | ||
<source lang="php"> | <source lang="php"> | ||
Line 24: | Line 23: | ||
</source> | </source> | ||
- | + | As you can see, we are calling for kApplication::recallObject method, which gets a link to required Event Handler by making call to another base object, link to which is accessible in the Factory property of Application object. | |
<source lang="php"> | <source lang="php"> | ||
Line 30: | Line 29: | ||
</source> | </source> | ||
- | + | In case if Event Handler object hasn't been created yet, then object of kFactory will create it for the first time while making another call to a base Application object to get and process Build Event which is triggered any time system creates a new Event instance: | |
<source lang="php"> | <source lang="php"> | ||
Line 46: | Line 45: | ||
- | == | + | == Main Phases in Application Process Flow == |
- | + | These are Initialization, Processing and Completion phases. Below is example from standard index.php file: | |
<source lang="php"> | <source lang="php"> | ||
- | $application =& kApplication::Instance(); - | + | $application =& kApplication::Instance(); - getting a link to Application object |
- | $application->Init(); - | + | $application->Init(); - initialization phase |
- | $application->Run(); - | + | $application->Run(); - processing phase |
- | $application->Done(); - | + | $application->Done(); - completion phase |
</source> | </source> | ||
- | == | + | == Initialization == |
+ | The key point of initialization in In-Portal is to get the most out your application by building strong and complete infrastructure. This done by initializing and creating all necessary objects, properly defining relations between them, and then processing all incoming data from HTTP requests (GET/POST/COOKIES and others) and finally properly apply all this data on the Web-server, database and application itself. | ||
- | + | === Connecting to Database and Creating Connection object === | |
- | === | + | Specifically for this task there is a code inside of kApplication::Init() method: |
- | + | ||
- | + | ||
<source lang="php"> | <source lang="php"> | ||
- | $this->Conn = new kDBConnection(SQL_TYPE, Array(&$this, 'handleSQLError') ); // | + | $this->Conn = new kDBConnection(SQL_TYPE, Array(&$this, 'handleSQLError') ); // new object |
$this->Conn->debugMode = $this->isDebugMode(); | $this->Conn->debugMode = $this->isDebugMode(); | ||
- | $this->Conn->Connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB); // | + | $this->Conn->Connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB); // connecting to the database |
</source> | </source> | ||
- | + | Connection to the database is handled through the object of kDBConnection class. Once created, a link to a database object is placed inside of Conn property of the main Application object. In it's turn, this new object of kDBConnection class is getting a link-back to the main Application object in it's construction method using the following code: | |
<source lang="php"> | <source lang="php"> | ||
Line 76: | Line 74: | ||
</source> | </source> | ||
- | + | ||
+ | Note that a link to database object can also be stored as a separate Conn property in other objects that tightly connected to database and can make often database calls. Good examples are objects of kDBBase class and his inheritors kDBItem and kDBList classes. This done for better performance: | ||
<source lang="php"> | <source lang="php"> | ||
Line 86: | Line 85: | ||
</source> | </source> | ||
- | + | Thus, once initialization is complete, the access to the database can be done virtually from any place: | |
<source lang="php"> | <source lang="php"> | ||
Line 93: | Line 92: | ||
- | === | + | === Creation of Factory Object and Registering New Classes === |
- | + | There is a code inside of kApplication::Init() method: | |
<source lang="php"> | <source lang="php"> | ||
Line 101: | Line 100: | ||
</source> | </source> | ||
- | + | Factory object is one of the key objects in the entire Application. It's designated to keep track of other objects that already been created and return a link to it in case if one is requested by the Application. It's obvious that object of kFactory class is created before any other standard objects. | |
- | + | In-Portal wides uses '''Late Initialization''' approach. Objects are created only when they are needed or required by someone. kFactory | |
+ | class provides a functionality when Late Initialization is done automatically as long as proper classes are registered and will be used in creation of its objects. Class Registration is quick and low resource operation which simply saves in properties (array format) of Factory object the following data: | ||
- | * | + | * pseudo-name of class: |
- | * | + | * path on the file-system to actual class |
- | * | + | * list of other classes that must be defined before this class |
- | + | Right after creation of Factory object system will continue registering other base classes critical to the application. This is done through the following method: | |
<source lang="php"> | <source lang="php"> | ||
Line 115: | Line 115: | ||
</source> | </source> | ||
- | + | Note that any of these Default classes registered via kFactory class can be replaced at later point of initialization based on your application needs. | |
+ | |||
+ | [[en:{{FULLPAGENAME}}]] | ||
+ | [[ru:K4:Протекание Процессов в Приложении]] | ||
+ | |||
+ | Translated from [http://guide.in-portal.org/rus/index.php?title=K4:%D0%9F%D1%80%D0%BE%D1%82%D0%B5%D0%BA%D0%B0%D0%BD%D0%B8%D0%B5_%D0%9F%D1%80%D0%BE%D1%86%D0%B5%D1%81%D1%81%D0%BE%D0%B2_%D0%B2_%D0%9F%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B8&oldid=1033 (revision 1033)] |
Current revision
This article is not finished yet! You see this message because current Article is finished yet or contains unverified information. How to write an Article. |
| ||
---|---|---|
Статьи в этой категории | ||
|
Contents |
kApplication - main class of the system
The object of kApplication class is created one of the first ones and among all key properties contains links to other important classes such as Session, TemplateParser, kFactory, kHTTPQuery, kEventManager and others. At the same time, almost all other objects in the system have a back-link to the main Application object. This allows flexible interaction between different parts of the system.
Example below, demonstrates that this kind of approach (when main application object links to other key objects) allows to call any type of event from any part of the system.
$event = new kEvent('custom-sections:OnAddListingLog'); $event->setEventParam('mode', LISTING_MODE_EXTERNAL); $this->Application->HandleEvent($event);
Method kApplication::HandleEvent passes the actual event processing to an object of kEventManager class. A link to that object is stored inside of application $this->Application->EventManager property.
At the same time, for event processing from kEventManager::HandleEvent method system makes calls to other methods of the main Application class. For example, in order to get the object of Event Handler for specific event we are using the following:
$event_handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); /* @var $event_handler kEventHandler */ $event_handler->processEvent($event);
As you can see, we are calling for kApplication::recallObject method, which gets a link to required Event Handler by making call to another base object, link to which is accessible in the Factory property of Application object.
$result =& $this->Factory->getObject($name, $pseudo_class, $event_params);
In case if Event Handler object hasn't been created yet, then object of kFactory will create it for the first time while making another call to a base Application object to get and process Build Event which is triggered any time system creates a new Event instance:
$event =& $this->Application->EventManager->getBuildEvent($pseudo_class); if($event) { $event->Init($prefix,$special); foreach($event_params as $param_name=>$param_value) { $event->setEventParam($param_name,$param_value); } $this->Application->HandleEvent($event); }
Main Phases in Application Process Flow
These are Initialization, Processing and Completion phases. Below is example from standard index.php file:
$application =& kApplication::Instance(); - getting a link to Application object $application->Init(); - initialization phase $application->Run(); - processing phase $application->Done(); - completion phase
Initialization
The key point of initialization in In-Portal is to get the most out your application by building strong and complete infrastructure. This done by initializing and creating all necessary objects, properly defining relations between them, and then processing all incoming data from HTTP requests (GET/POST/COOKIES and others) and finally properly apply all this data on the Web-server, database and application itself.
Connecting to Database and Creating Connection object
Specifically for this task there is a code inside of kApplication::Init() method:
$this->Conn = new kDBConnection(SQL_TYPE, Array(&$this, 'handleSQLError') ); // new object $this->Conn->debugMode = $this->isDebugMode(); $this->Conn->Connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB); // connecting to the database
Connection to the database is handled through the object of kDBConnection class. Once created, a link to a database object is placed inside of Conn property of the main Application object. In it's turn, this new object of kDBConnection class is getting a link-back to the main Application object in it's construction method using the following code:
$this->Application =& kApplication::Instance();
Note that a link to database object can also be stored as a separate Conn property in other objects that tightly connected to database and can make often database calls. Good examples are objects of kDBBase class and his inheritors kDBItem and kDBList classes. This done for better performance:
function kDBBase() { parent::kBase(); $this->Conn =& $this->Application->GetADODBConnection(); }
Thus, once initialization is complete, the access to the database can be done virtually from any place:
$all_tables = $this->Conn->Query('SHOW TABLES');
Creation of Factory Object and Registering New Classes
There is a code inside of kApplication::Init() method:
$this->Factory = new kFactory();
Factory object is one of the key objects in the entire Application. It's designated to keep track of other objects that already been created and return a link to it in case if one is requested by the Application. It's obvious that object of kFactory class is created before any other standard objects.
In-Portal wides uses Late Initialization approach. Objects are created only when they are needed or required by someone. kFactory
class provides a functionality when Late Initialization is done automatically as long as proper classes are registered and will be used in creation of its objects. Class Registration is quick and low resource operation which simply saves in properties (array format) of Factory object the following data:
- pseudo-name of class:
- path on the file-system to actual class
- list of other classes that must be defined before this class
Right after creation of Factory object system will continue registering other base classes critical to the application. This is done through the following method:
$this->RegisterDefaultClasses();
Note that any of these Default classes registered via kFactory class can be replaced at later point of initialization based on your application needs.
Translated from (revision 1033)