Skip to content

Commit

Permalink
Extract Application::init() listeners bits and drop the rest
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Khudiakov <[email protected]>
  • Loading branch information
Xerkus committed Jun 11, 2020
1 parent 0676a1d commit 02f72d5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
56 changes: 5 additions & 51 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public function __construct(
$this->setEventManager($events ?: $serviceManager->get('EventManager'));
$this->request = $request ?: $serviceManager->get('Request');
$this->response = $response ?: $serviceManager->get('Response');

foreach ($this->defaultListeners as $listener) {
$serviceManager->get($listener)->attach($this->events);
}
}

/**
Expand All @@ -129,21 +133,13 @@ public function getConfig()
* router. Attaches the ViewManager as a listener. Triggers the bootstrap
* event.
*
* @param array $listeners List of listeners to attach.
* @return Application
*/
public function bootstrap(array $listeners = [])
public function bootstrap()
{
$serviceManager = $this->serviceManager;
$events = $this->events;

// Setup default listeners
$listeners = array_unique(array_merge($this->defaultListeners, $listeners));

foreach ($listeners as $listener) {
$serviceManager->get($listener)->attach($events);
}

// Setup MVC Event
$this->event = $event = new MvcEvent();
$event->setName(MvcEvent::EVENT_BOOTSTRAP);
Expand Down Expand Up @@ -227,48 +223,6 @@ public function getEventManager()
return $this->events;
}

/**
* Static method for quick and easy initialization of the Application.
*
* If you use this init() method, you cannot specify a service with the
* name of 'ApplicationConfig' in your service manager config. This name is
* reserved to hold the array from application.config.php.
*
* The following services can only be overridden from application.config.php:
*
* - ModuleManager
* - SharedEventManager
* - EventManager & Laminas\EventManager\EventManagerInterface
*
* All other services are configured after module loading, thus can be
* overridden by modules.
*
* @param array $configuration
* @return Application
*/
public static function init($configuration = [])
{
// Prepare the service manager
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : [];
$smConfig = new Service\ServiceManagerConfig($smConfig);

$serviceManager = new ServiceManager();
$smConfig->configureServiceManager($serviceManager);
$serviceManager->setService('ApplicationConfig', $configuration);

// Load modules
$serviceManager->get('ModuleManager')->loadModules();

// Prepare list of listeners to bootstrap
$listenersFromAppConfig = isset($configuration['listeners']) ? $configuration['listeners'] : [];
$config = $serviceManager->get('config');
$listenersFromConfigService = isset($config['listeners']) ? $config['listeners'] : [];

$listeners = array_unique(array_merge($listenersFromConfigService, $listenersFromAppConfig));

return $serviceManager->get('Application')->bootstrap($listeners);
}

/**
* Run the application
*
Expand Down
14 changes: 13 additions & 1 deletion src/Service/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Interop\Container\ContainerInterface;
use Laminas\Mvc\Application;
use Laminas\ServiceManager\Factory\FactoryInterface;
use function is_string;

class ApplicationFactory implements FactoryInterface
{
Expand All @@ -27,11 +28,22 @@ class ApplicationFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
return new Application(
$application = new Application(
$container,
$container->get('EventManager'),
$container->get('Request'),
$container->get('Response')
);

if (!$container->has('config')) {
return $application;
}

$em = $application->getEventManager();
$listeners = $container->get('config')[Application::class]['listeners'] ?? [];
foreach ($listeners as $listener) {
$container->get($listener)->attach($em);
}
return $application;
}
}

0 comments on commit 02f72d5

Please sign in to comment.