⬆️ ⬇️

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 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.



Run fools!



A warning



  1. 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.
  2. 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] .
  3. 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



Source , a little [mine] .





Framework device



General structure and communication



A source



When 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; } } 


Plugins



A source



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



Visualization



[my]





Connections



connections



Scheme



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 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.



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:



Caution, spoiler!

Blog



In three articles c Habra



  • https://habr.com/post/192522/
  • Free translation of simple blog development documentation on ZendSkeletonApplication. It tells about configs, ServiceManager (or ModuleManager, I did not understand) and the connection of third-party libraries.


Original blog documentation





Eventmanager



Primary review





Detailed analysis





ServiceManager



Quick start





Detailed analysis





ModuleManager



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:



Kitty



(= ^ ・ Ω ・ ^ =)



')

Source: https://habr.com/ru/post/426303/



All Articles