- Magento 2 Developer's Guide
- Branko Ajzele
- 493字
- 2021-07-30 09:53:23
Code generation
One of the neat features of the Magento application is code generation. Code generation, as implied by its name, generates nonexistent classes. These classes are generated in Magento's var/generation
directory.
The directory structure within var/generation
is somewhat similar to that of the core vendor/magento/module-*
and app/code
directories. To be more precise, it follows the module structure. The code is generated for something that is called Factory, Proxy, and Interceptor classes.
The Factory class creates an instance of a type. For example, a var/generation/Magento/Catalog/Model/ProductFactory.php
file with a Magento\Catalog\Model\ProductFactory
class has been created because somewhere within the vendor/magento
directory and its code, there is a call to the Magento\Catalog\Model\ProductFactory
class, which originally does not exist in Magento. During runtime, when {someClassName}Factory
is called in the code, Magento creates a Factory class under the var/generation
directory if it does not exist. The following code is an example of the (partial) ProductFactory
class:
namespace Magento\Catalog\Model; /** * Factory class for @see \Magento\Catalog\Model\Product */ class ProductFactory { //... /** * Create class instance with specified parameters * * @param array $data * @return \Magento\Catalog\Model\Product */ public function create(array $data = array()) { return $this->_objectManager->create($this->_instanceName, $data); } }
Note the create
method that creates and returns the Product
type instance. Also, note how the generated code is type safe providing @return
annotation for integrated development environments (IDEs) to support the autocomplete functionality.
Factories are used to isolate an object manager from the business code. Factories can be dependent on the object manager, unlike business objects.
The Proxy class is a wrapper for some base class. Proxy classes provide better performance than the base classes because they can be instantiated without instantiating a base class. A base class is instantiated only when one of its methods is called. This is highly convenient for cases where the base class is used as a dependency, but it takes a lot of time to instantiate, and its methods are used only during some paths of execution.
Like Factory, the Proxy classes are also generated under the var/generation
directory.
If we were to take a look at the var/generation/Magento/Catalog/Model/Session/Proxy.php
file that contains the Magento\Catalog\Model\Session\Proxy
class, we would see that it actually extends \Magento\Catalog\Model\Session
. The wrapping Proxy class implements several magical methods along the way, such as __sleep
, __wakeup
, __clone
, and __call
.
Interceptor is yet another class type that gets autogenerated by Magento. It is related to the plugins feature, which will be discussed in detail later in Chapter 6, Plugins.
In order to trigger code regeneration, we can use the code compiler that is available on the console. We can run either the single-tenant compiler or the multi-tenant compiler.
The single-tenant implies one website and store, and it is executed by using the following command:
magento setup:di:compile
The multi-tenant implies more than one independent Magento application, and it is executed by using following command.
magento setup:di:compile-multi-tenant
Code compilation generates factories, proxies, interceptors, and several other classes, as listed in the setup/src/Magento/Setup/Module/Di/App/Task/Operation/
directory.
- Vue.js 3.x快速入門
- Clojure for Domain:specific Languages
- Learning AWS Lumberyard Game Development
- Spring Cloud、Nginx高并發(fā)核心編程
- Java Web應(yīng)用開發(fā)技術(shù)與案例教程(第2版)
- SAS數(shù)據(jù)統(tǒng)計分析與編程實踐
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- PostgreSQL Replication(Second Edition)
- Learning Zurb Foundation
- Kubernetes源碼剖析
- Windows Phone 8 Game Development
- 小程序從0到1:微信全棧工程師一本通
- jQuery for Designers Beginner's Guide Second Edition
- Arduino電子設(shè)計實戰(zhàn)指南:零基礎(chǔ)篇
- Data Manipulation with R(Second Edition)