This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
/
Module.php
73 lines (67 loc) · 2.65 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Api;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ControllerProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
class Module implements ConfigProviderInterface, BootstrapListenerInterface,
ServiceProviderInterface, ControllerProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(EventInterface $e)
{
$app = $e->getTarget();
$events = $app->getEventManager();
$services = $app->getServiceManager();
$events->attach('route', function ($e) use ($services) {
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
if ($controller != 'Api\UserController') {
return;
}
$view = $services->get('View');
$jsonStrategy = $services->get('ViewJsonStrategy');
$view->getEventManager()->attach($jsonStrategy, 100);
}, -10);
}
public function getServiceConfig()
{
return array('factories' => array(
'Api\UserStorage' => function ($services) {
$config = $services->get('Config');
if (!isset($config['api'])
|| !isset($config['api']['cla_users_path'])
) {
throw new ServiceNotCreatedException('Missing configuration for CLA user data path');
}
$path = $config['api']['cla_users_path'];
try {
$storage = new UserStorage($path);
} catch (\InvalidArgumentException $e) {
throw new ServiceNotCreatedException('Invalid path configuration for CLA user data path', $e->getCode(), $e);
}
return $storage;
},
'Api\UserService' => function ($services) {
$storage = $services->get('Api\UserStorage');
return new UserService($storage);
}
));
}
public function getControllerConfig()
{
return array('factories' => array(
'Api\UserController' => function ($controllers) {
$services = $controllers->getServiceLocator();
$controller = new UserController();
$controller->setUserService($services->get('Api\UserService'));
return $controller;
}
));
}
}