A brief tour of the highlights of the Zend Framework
Is it just a framework, or does this framework epitomize the pride of the PHP community — its hard-working developers, the key ingredient, so to speak? With a scattering of configs ... The object of love of our PL, with a good MVC, thanks to which Zend Framework is the best PHP framework.
Here you will not find the answer to this question, but you will learn about the ServiceManager and ModuleManager.
A warning
- This material is based on what I was looking for on the Zend Framework 2, in some places even mentions in version 1 appear. *. I do not think that this will become a problem when compared with other versions, since fundamental points are considered and they are unlikely to change globally.
- In the arguments and translations (as well as in paragraph 1) there may be blunders, both mine and original sources. All will be given links, and their own creativity will be with a note [mine] .
- Focused on those who googled and, like me, confused. There will be no deployment of blogs, explanations and interactive. But there will be two pictures and a kitty.
Content
- Terms
- Framework device
- General structure and connections
- Plugins
- Visualization
- Connections
- Schematic representation
- From the author
- useful links
- Bonus
Terms
Source , a little [mine] .
- Application - the final product site;
- Module - a functionally complete "block" of an application, whose code may consist of models, views, controllers. The module extends the functionality of a web application and can function only "inside" it; Modules are registered in
application.config.php
the section.modules
- ModuleManager - a container for manipulating modules;
- Service - "mechanism" in the module, for manipulations between models, controllers, types, etc.; Services are registered in
module.config.php
the sectionservice_manager
. - ServiceManager - a container for service manipulations.
- ControllerManager - works with services and factories for loading controllers (
\Zend\ServiceManager\AbstractFactoryInterface
or\Zend\ServiceManager\ServiceManager
). dock - EventManager is a component that aggregates event handlers (Listener) for one or more named events (Event), and also initiates the processing of these events.
- A plugin is a class that in some way extends the functionality of all controllers.
Framework device
General structure and connections
When created Zend\Mvc\Application
, an object Zend\ServiceManager\ServiceManager
is created and configured via Zend\Mvc\Service\ServiceManagerConfig
. ServiceManagerConfig
gets the configuration from config/application.config.php
(or some other config of the application, which is passed in Application
when it is created). Of all the services and factories represented in the namespace Zend\Mvc\Service
, ServiceManagerConfig
only three are responsible: SharedEventManager
,, EventManager
and ModuleManager
.
After that Application
retrieves ModuleManager
. At this moment ModuleManager
through ServiceManager
configures the services and factories provided in Zend\Mvc\Service\ServiceListenerFactory
. This approach makes it possible to simplify the configuration of the main application as much as possible and to provide the developer with the ability to configure various parts of the MVC system from modules, overriding any default configuration in the services of these MVCs.
ModuleManager
, Expressed in Zend\Mvc\Service\ModuleManagerFactory
. This is probably the most complex factory in the MVC stack. ModuleManager
expects the service to be ApplicationConfig
implemented ( Di ) with keys module_listener_options
and modules
.
It creates an instance Zend\ModuleManager\Listener\DefaultListenerAggregate
using the extracted module_listener_options
. It then checks if the service exists with the name ServiceListener
; if not, it uses the factory with the name Zend\Mvc\Service\ServiceListenerFactory
. As ServiceListener
will be added to multiple services listeners, such as methods of listeners getServiceConfig
, getControllerConfig
, getControllerPluginConfig
, getViewHelperConfig
module.
Then ModuleManager
retrieves the service EventManager
and attaches the aforementioned listeners. It creates an instance Zend\ModuleManager\ModuleEvent
by setting the "ServiceManager" parameter in the service manager object. Finally, it instantiates Zend\ModuleManager\ModuleManager
and injects EventManager
and ModuleEvent
.
[my] The case when the code is clearer:
<?phpnamespaceZend\Mvc\Service;
useZend\ModuleManager\Listener\DefaultListenerAggregate;
useZend\ModuleManager\Listener\ListenerOptions;
useZend\ModuleManager\ModuleEvent;
useZend\ModuleManager\ModuleManager;
useZend\ServiceManager\FactoryInterface;
useZend\ServiceManager\ServiceLocatorInterface;
classModuleManagerFactoryimplementsFactoryInterface{
publicfunctioncreateService(ServiceLocatorInterface $serviceLocator){
if (!$serviceLocator->has('ServiceListener')) {
$serviceLocator->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');
}
$configuration = $serviceLocator->get('ApplicationConfig');
$listenerOptions = new ListenerOptions($configuration['module_listener_options']);
$defaultListeners = new DefaultListenerAggregate($listenerOptions);
$serviceListener = $serviceLocator->get('ServiceListener');
$serviceListener->addServiceManager(
$serviceLocator,
'service_manager',
'Zend\ModuleManager\Feature\ServiceProviderInterface',
'getServiceConfig'
); // то же самое для остальных методов модуля
$events = $serviceLocator->get('EventManager');
$events->attach($defaultListeners);
$events->attach($serviceListener);
$moduleEvent = new ModuleEvent;
$moduleEvent->setParam('ServiceManager', $serviceLocator);
$moduleManager = new ModuleManager($configuration['modules'], $events);
$moduleManager->setEvent($moduleEvent);
return $moduleManager;
}
}
Plugins
The controller architecture includes a plugin system that allows you to add your own code, which will be called when certain events occur during the life of the controller. The front controller uses the plugin broker as a registry of user plug-ins, the plug-in broker also provides a call to event methods in each plugin registered through the front controller.
Event methods are defined in an abstract class Zend_Controller_Plugin_Abstract
from which all user plugins should inherit.
Visualization
[my]
- Reads from top to bottom, unless otherwise specified by arrows.
- Lines with arrows indicate what is included.
- Thin lines without arrows indicate what is connected with what.
- Thick lines with no arrows indicate what controls.
Connections
Scheme
From the author
The attentive reader noted that the article begins with a link to the Toaster, where a question is asked about the differences between ServiceManager and ModuleManager, and the text of the article begins with them. Coincidence? I do not think. The fact is that Habr became the first place where I started to get acquainted with the basics of the framework and confusion was introduced by the publication where the blog was recreated from the documentation with comments from the author of the article. It was the lack of a description of the ModuleManager that pushed me to the wrong reasoning (that modules are registered in the ServiceManager) and this led to the writing of this article.
useful links
I do not want to engage in copy-paste and grow 6 parts per subject, so I attach a list of my bookmarks on ZF with notes:
Blog
In three articles c Habra
- https://habr.com/post/192522/
- Вольный перевод документации по разработке простого блога на ZendSkeletonApplication. Рассказывается о конфигах, ServiceManager (или ModuleManager, я так и не понял) и подключении сторонних библиотек.
Оригинал документации на блог
EventManager
Первичный обзор
- https://habr.com/post/131077/
- Быстрый обзор нового (на момент публикации) инструмента.
Подробный разбор
- http://zf2.com.ua/doc/50
- Документация из украинского сообщества по ZF.
ServiceManager
Quick start
- http://zf2.com.ua/doc/64, http://zf2.com.ua/doc/103
- Документация из украинского сообщества по ZF.
Подробный разбор
- https://habr.com/post/241471/
- Invokable, фабрики и другие непонятные штуки.
ModuleManager
Документация
- http://zf2.com.ua/doc/98
- Документация из украинского сообщества по ZF.
I hope the brief excursion didn’t take too long and was useful. Of course, edits, suggestions, criticisms and other actions permitted by the Habr rules and the current legislation of the Russian Federation are accepted on your part.
The bonus we deserve:
Kitty
(= ^ ・ Ω ・ ^ =)