Skip to content

Commit acd4e9a

Browse files
committed
User form is created by factory now.
1 parent badd2f7 commit acd4e9a

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

config/module.config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace ProspectOne\UserModule;
33

44
use ProspectOne\UserModule\Factory\BcryptFactory;
5+
use ProspectOne\UserModule\Form\Factory\UserFormFactory;
6+
use ProspectOne\UserModule\Form\UserForm;
57
use ProspectOne\UserModule\Service\Factory\CurrentUserFactory;
68
use Zend\Router\Http\Literal;
79
use Zend\Router\Http\Segment;
@@ -88,6 +90,7 @@
8890
Service\UserManager::class => Service\Factory\UserManagerFactory::class,
8991
'ProspectOne\UserModule\Bcrypt' => BcryptFactory::class,
9092
'ProspectOne\UserModule\CurrentUser' => CurrentUserFactory::class,
93+
UserForm::class => UserFormFactory::class,
9194
],
9295
],
9396
'view_manager' => [

src/Controller/Factory/UserControllerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
1818
$userManager = $container->get(UserManager::class);
1919

2020
// Instantiate the controller and inject dependencies
21-
return new UserController($entityManager, $userManager);
21+
return new UserController($entityManager, $userManager, $container);
2222
}
2323
}

src/Controller/UserController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ProspectOne\UserModule\Service\UserManager;
77
use Zend\Hydrator\ClassMethods;
88
use Zend\Mvc\Controller\AbstractActionController;
9+
use Zend\ServiceManager\ServiceLocatorInterface;
910
use Zend\View\Model\ViewModel;
1011
use ProspectOne\UserModule\Entity\User;
1112
use ProspectOne\UserModule\Form\UserForm;
@@ -32,15 +33,22 @@ class UserController extends AbstractActionController
3233
*/
3334
private $userManager;
3435

36+
/**
37+
* @var ServiceLocatorInterface
38+
*/
39+
private $container;
40+
3541
/**
3642
* Constructor.
3743
* @param EntityManager $entityManager
3844
* @param UserManager $userManager
45+
* @param ServiceLocatorInterface $container
3946
*/
40-
public function __construct(EntityManager $entityManager, UserManager $userManager)
47+
public function __construct(EntityManager $entityManager, UserManager $userManager, ServiceLocatorInterface $container)
4148
{
4249
$this->entityManager = $entityManager;
4350
$this->userManager = $userManager;
51+
$this->container = $container;
4452
}
4553

4654
/**
@@ -65,7 +73,7 @@ public function addAction()
6573
$rolesselector = $this->getRolesSelector();
6674

6775
// Create user form
68-
$form = new UserForm('create', $this->entityManager, null, $rolesselector, self::GUEST_ROLE_ID);
76+
$form = $this->container->build(UserForm::class, ['create', $this->entityManager, null, $rolesselector, self::GUEST_ROLE_ID]);
6977

7078
// Check if user has submitted the form
7179
if ($this->getRequest()->isPost()) {
@@ -144,7 +152,7 @@ public function editAction()
144152
$rolecurrent = $this->getUserRole($user);
145153

146154
// Create user form
147-
$form = new UserForm('update', $this->entityManager, $user, $rolesselector, $rolecurrent);
155+
$form = $this->container->build(UserForm::class, ['update', $this->entityManager, $user, $rolesselector, $rolecurrent]);
148156

149157
// Check if user has submitted the form
150158
if ($this->getRequest()->isPost()) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace ProspectOne\UserModule\Form\Factory;
4+
5+
use Doctrine\ORM\EntityManager;
6+
use Interop\Container\ContainerInterface;
7+
use Interop\Container\Exception\ContainerException;
8+
use ProspectOne\UserModule\Entity\User;
9+
use ProspectOne\UserModule\Form\UserForm;
10+
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
11+
use Zend\ServiceManager\Exception\ServiceNotFoundException;
12+
use Zend\ServiceManager\Factory\FactoryInterface;
13+
14+
/**
15+
* Class UserFormFactory
16+
* @package ProspectOne\UserModule\Form\Factory
17+
*/
18+
class UserFormFactory implements FactoryInterface
19+
{
20+
/**
21+
* @var ContainerInterface
22+
*/
23+
private $container;
24+
25+
/**
26+
* UserFormFactory constructor.
27+
* @param ContainerInterface|null $container
28+
*/
29+
public function __construct(?ContainerInterface $container = null)
30+
{
31+
if (!empty($container)) {
32+
$this->container = $container;
33+
}
34+
}
35+
36+
/**
37+
* Create an object
38+
*
39+
* @param ContainerInterface $container
40+
* @param string $requestedName
41+
* @param null|array $options
42+
* @return object
43+
* @throws ServiceNotFoundException if unable to resolve the service.
44+
* @throws ServiceNotCreatedException if an exception is raised when
45+
* creating a service.
46+
* @throws ContainerException if any other error occurs
47+
*/
48+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
49+
{
50+
if(empty($options)) {
51+
return new UserForm();
52+
}
53+
54+
return new UserForm(...$options);
55+
}
56+
}

0 commit comments

Comments
 (0)