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 lot of configs ... The object of love of our PL, with a good MVC, so that 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.
Source , a little [mine] .
application.config.php
in the modules
section.module.config.php
in the service_manager
section.\Zend\ServiceManager\AbstractFactoryInterface
or \Zend\ServiceManager\ServiceManager
). dockWhen a Zend\Mvc\Application
, a Zend\ServiceManager\ServiceManager
object is created and configured via Zend\Mvc\Service\ServiceManagerConfig
. ServiceManagerConfig
gets the configuration from config/application.config.php
(or some other application config that is passed to Application
when it is created). Of all the services and factories represented in the Zend\Mvc\Service
namespace, ServiceManagerConfig
is responsible for only three: SharedEventManager
, EventManager
and ModuleManager
.
After this, the Application
retrieves the ModuleManager
. At this point, the ModuleManager
through the 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 allow the developer 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 ApplicationConfig
service to be implemented ( Di ) with the module_listener_options
and modules
keys.
It creates an instance of Zend\ModuleManager\Listener\DefaultListenerAggregate
using the extracted module_listener_options
. It then checks if a service exists with the name ServiceListener
; if not, it uses a factory named Zend\Mvc\Service\ServiceListenerFactory
. A number of listener services will be added to the ServiceListener
, such as listeners of the getServiceConfig
, getControllerConfig
, getControllerPluginConfig
, getViewHelperConfig
module getViewHelperConfig
.
The ModuleManager
then retrieves the ModuleManager
service and attaches the above listeners. It creates an instance of Zend\ModuleManager\ModuleEvent
, by setting the "ServiceManager" parameter to the service manager object. Finally, it creates an instance of Zend\ModuleManager\ModuleManager
and implements the EventManager
and ModuleEvent
.
[my] The case when the code is clearer:
<?php namespace Zend\Mvc\Service; use Zend\ModuleManager\Listener\DefaultListenerAggregate; use Zend\ModuleManager\Listener\ListenerOptions; use Zend\ModuleManager\ModuleEvent; use Zend\ModuleManager\ModuleManager; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ModuleManagerFactory implements FactoryInterface { public function createService(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; } }
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 for event methods in each plugin registered through the front controller.
Event methods are defined in the abstract class Zend_Controller_Plugin_Abstract
from which all user plugins should inherit
[my]
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 absence of the 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.
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:
In three articles c Habra
Original blog documentation
Primary review
Detailed analysis
Quick start
Detailed analysis
Documentation
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:
(= ^ ・ Ω ・ ^ =)
Source: https://habr.com/ru/post/426303/