Skip to content
This repository was archived by the owner on Jul 20, 2021. It is now read-only.

Commit 806c4d7

Browse files
committed
Don't make use of the servicemanager in contrroller but inject
dependencies
1 parent 8818742 commit 806c4d7

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

Module.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public function getServiceConfig()
3434
{
3535
return include __DIR__ . '/config/services.config.php';
3636
}
37+
38+
public function getControllerConfig()
39+
{
40+
return array(
41+
'factories' => array(
42+
'zfcuserlist' => 'ZfcUserList\Controller\Factory\UserListController',
43+
),
44+
);
45+
}
3746
}

config/module.config.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
'zfcuserlist' => __DIR__ . '/../view',
66
),
77
),
8-
'controllers' => array(
9-
'invokables' => array(
10-
'zfcuserlist' => 'ZfcUserList\Controller\UserListController',
11-
),
12-
),
138
'router' => array(
149
'routes' => array(
1510
'zfcuser' => [
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace ZfcUserList\Controller\Factory;
3+
4+
use Zend\ServiceManager\FactoryInterface;
5+
use Zend\ServiceManager\ServiceLocatorInterface;
6+
use ZfcUserList\Controller\UserListController;
7+
8+
class UserListController implements FactoryInterface
9+
{
10+
public function createService(ServiceLocatorInterface $sm)
11+
{
12+
$parentLocator = $sm->getServiceLocator();
13+
14+
$controller = new UserListController(
15+
$parentLocator->get("zfcuserlist_module_options"),
16+
$parentLocator->get("zfcuser_user_mapper")
17+
);
18+
return $controller;
19+
}
20+
}

src/ZfcUserList/Controller/UserListController.php

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@
1010

1111
class UserListController extends AbstractActionController
1212
{
13-
protected $options, $userMapper;
14-
protected $zfcUserOptions;
13+
protected $options;
14+
protected $userMapper;
15+
16+
public function __construct(ModuleOptions $options, UserInterface $userMapper)
17+
{
18+
$this->setOptions($options);
19+
$this->setUserMapper($userMapper);
20+
}
21+
1522
public function listAction()
1623
{
17-
$userMapper = $this->getUserMapper();
18-
$users = $userMapper->findAll();
24+
$users = $this->userMapper->findAll();
1925
if (is_array($users)) {
2026
$paginator = new Paginator\Paginator(new Paginator\Adapter\ArrayAdapter($users));
2127
} else {
2228
$paginator = $users;
2329
}
2430

25-
$paginator->setItemCountPerPage($this->getOptions()->getElementsPerPage());
31+
$paginator->setItemCountPerPage($this->options->getElementsPerPage());
2632
$paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
2733
return array(
2834
'users' => $paginator,
29-
'userlistElements' => $this->getOptions()->getUserListElements()
35+
'userlistElements' => $this->options->getUserListElements()
3036
);
3137
}
3238

@@ -36,41 +42,9 @@ public function setOptions(ModuleOptions $options)
3642
return $this;
3743
}
3844

39-
public function getOptions()
40-
{
41-
if (!$this->options instanceof ModuleOptions) {
42-
$this->setOptions($this->getServiceLocator()->get('zfcuserlist_module_options'));
43-
}
44-
return $this->options;
45-
}
46-
47-
public function getUserMapper()
48-
{
49-
if (null === $this->userMapper) {
50-
$this->userMapper = $this->getServiceLocator()->get('zfcuser_user_mapper');
51-
}
52-
return $this->userMapper;
53-
}
54-
5545
public function setUserMapper(UserInterface $userMapper)
5646
{
5747
$this->userMapper = $userMapper;
5848
return $this;
5949
}
60-
public function setZfcUserOptions(ZfcUserModuleOptions $options)
61-
{
62-
$this->zfcUserOptions = $options;
63-
return $this;
64-
}
65-
66-
/**
67-
* @return \ZfcUser\Options\ModuleOptions
68-
*/
69-
public function getZfcUserOptions()
70-
{
71-
if (!$this->zfcUserOptions instanceof ZfcUserModuleOptions) {
72-
$this->setZfcUserOptions($this->getServiceLocator()->get('zfcuser_module_options'));
73-
}
74-
return $this->zfcUserOptions;
75-
}
7650
}

0 commit comments

Comments
 (0)