1 <?php
2
3 namespace Balance\Mvc\Controller;
4
5 use Zend\Mvc\Controller\AbstractActionController;
6 use Zend\ServiceManager\AbstractFactoryInterface;
7 use Zend\ServiceManager\ServiceLocatorInterface;
8
9 10 11
12 class AbstractControllerFactory implements AbstractFactoryInterface
13 {
14 15 16
17 public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
18 {
19
20 $config = $serviceLocator->getServiceLocator()->get('Config')['balance_manager']['factories'];
21
22 return
23 isset($config[$requestedName])
24 && class_exists($requestedName)
25 && is_subclass_of($requestedName, AbstractActionController::CLASS)
26 && isset($config[$requestedName]['factory'])
27 && $config[$requestedName]['factory'] === __CLASS__;
28 }
29
30 31 32
33 public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
34 {
35
36 $parentServiceLocator = $serviceLocator->getServiceLocator();
37
38 $config = $parentServiceLocator->get('Config')['balance_manager']['factories'][$requestedName];
39
40
41 $controller = new $requestedName();
42
43
44 if ($controller instanceof ModelAwareInterface) {
45
46 $model = $parentServiceLocator->get($config['params']['model']);
47
48 $controller->setModel($model);
49 }
50
51
52 if ($controller instanceof RedirectRouteNameAwareInterface) {
53
54 $redirectRouteName = $config['params']['redirect_route_name'];
55
56 $controller->setRedirectRouteName($redirectRouteName);
57 }
58
59
60 return $controller;
61 }
62 }
63