Skip to content

Commit 94d9c5c

Browse files
author
Codeliner
committed
Add controller, views and form to handle cargos
1 parent 7ac61dc commit 94d9c5c

File tree

9 files changed

+394
-12
lines changed

9 files changed

+394
-12
lines changed

config/autoload/global.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
'invokables' => array(
1313
'Doctrine\ORM\Mapping\UnderscoreNamingStrategy' => 'Doctrine\ORM\Mapping\UnderscoreNamingStrategy',
1414
),
15-
'aliases' => array(
16-
'entitymanager' => 'doctrine.entitymanager.orm_default',
17-
),
1815
),
1916
'doctrine' => array(
2017
'configuration' => array(

module/Application/config/module.config.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,33 @@
4646
'defaults' => array(
4747
),
4848
),
49+
'may_terminate' => true,
50+
'child_routes' => array(
51+
'trackingid' => array(
52+
'type' => 'Segment',
53+
'options' => array(
54+
'route' => '/trackingid/[:trackingid]',
55+
'constraints' => array(
56+
'trackingid' => '[a-zA-Z0-9_-]*',
57+
),
58+
'defaults' => array(
59+
),
60+
),
61+
),
62+
),
4963
),
5064
),
5165
),
5266
),
5367
),
5468
'service_manager' => array(
5569
'factories' => array(
56-
'main_navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
70+
'main_navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
71+
'cargo_form' => 'Application\Form\Service\CargoFormFactory',
72+
'cargo_repository' => function($sl) {
73+
$em = $sl->get('doctrine.entitymanager.orm_default');
74+
return $em->getRepository('Application\Domain\Model\Cargo\Cargo');
75+
},
5776
),
5877
'abstract_factories' => array(
5978
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
@@ -77,6 +96,18 @@
7796
'invokables' => array(
7897
'Application\Controller\Index' => 'Application\Controller\IndexController'
7998
),
99+
'factories' => array(
100+
'Application\Controller\Cargo' => function($controllerLoader) {
101+
$serviceManager = $controllerLoader->getServiceLocator();
102+
103+
$cargoRepository = $serviceManager->get('cargo_repository');
104+
105+
$cargoController = new Application\Controller\CargoController();
106+
$cargoController->setCargoRepository($cargoRepository);
107+
$cargoController->setCargoForm($serviceManager->get('cargo_form'));
108+
return $cargoController;
109+
}
110+
)
80111
),
81112
'view_manager' => array(
82113
'display_not_found_reason' => true,
@@ -95,7 +126,7 @@
95126
),
96127
),
97128
'doctrine' => array(
98-
'connection' => array(
129+
'configuration' => array(
99130
'orm_default' => array(
100131
//Define custom doctrine types to map the ddd value objects
101132
'types' => array(
@@ -104,12 +135,44 @@
104135
),
105136
),
106137
),
138+
'driver' => array(
139+
'application_module_driver' => array(
140+
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
141+
'cache' => 'array',
142+
'paths' => array(
143+
__DIR__ . '/../src/Application/Domain/Model/Cargo/'
144+
)
145+
),
146+
'orm_default' => array(
147+
'drivers' => array(
148+
'Application' => 'application_module_driver',
149+
)
150+
)
151+
)
107152
),
108153
'navigation' => array(
109154
'default' => array(
110155
'home' => array(
111156
'route' => 'home',
112157
'label' => 'home'
158+
),
159+
'cargo' => array(
160+
'type' => 'uri',
161+
'label' => 'Cargo',
162+
'pages' => array(
163+
'list' => array(
164+
'route' => 'application/default',
165+
'controller' => 'cargo',
166+
'action' => 'index',
167+
'label' => 'list Cargos'
168+
),
169+
'add' => array(
170+
'route' => 'application/default',
171+
'controller' => 'cargo',
172+
'action' => 'add',
173+
'label' => 'add Cargo'
174+
)
175+
)
113176
)
114177
)
115178
),
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Controller;
10+
11+
use Zend\Mvc\Controller\AbstractActionController;
12+
use Zend\View\Model\ViewModel;
13+
use Application\Domain\Model\Cargo;
14+
use Application\Form\CargoForm;
15+
/**
16+
* MVC Controller for Cargo Management
17+
*
18+
* @author Alexander Miertsch <[email protected]>
19+
*/
20+
class CargoController extends AbstractActionController
21+
{
22+
/**
23+
* The CargoRepository
24+
*
25+
* @var Cargo\CargoRepositoryInterface
26+
*/
27+
protected $cargoRepository;
28+
29+
/**
30+
*
31+
* @var CargoForm
32+
*/
33+
protected $cargoForm;
34+
35+
36+
public function indexAction()
37+
{
38+
$cargos = $this->cargoRepository->findAll();
39+
40+
return new ViewModel(array('cargos' => $cargos));
41+
}
42+
43+
public function showAction()
44+
{
45+
$trackingId = $this->getEvent()->getRouteMatch()->getParam('trackingid');
46+
47+
if (is_null($trackingId)) {
48+
throw new \InvalidArgumentException('Cargo can not be found. TrackingId missing!');
49+
}
50+
51+
$trackingId = new Cargo\TrackingId($trackingId);
52+
53+
$cargo = $this->cargoRepository->findCargo($trackingId);
54+
55+
if (is_null($cargo)) {
56+
throw new \RuntimeException('Cargo can not be found. Please check the trackingId!');
57+
}
58+
59+
return array('cargo' => $cargo);
60+
}
61+
62+
public function addAction()
63+
{
64+
//we use the post redirect get pattern and redirect to same location
65+
$prg = $this->prg();
66+
67+
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
68+
// returned a response to redirect us
69+
return $prg;
70+
} elseif ($prg === false) {
71+
// this wasn't a POST request
72+
// probably this is the first time the form was loaded
73+
return array('form' => $this->cargoForm);
74+
}
75+
76+
$newCargo = new Cargo\Cargo($this->cargoRepository->getNextTrackingId());
77+
$this->cargoForm->bind($newCargo);
78+
$this->cargoForm->setData($prg);
79+
80+
if ($this->cargoForm->isValid()) {
81+
$this->cargoRepository->store($newCargo);
82+
83+
return $this->redirect()->toRoute(
84+
'application/default',
85+
array(
86+
'controller' => 'cargo',
87+
'action' => 'index'
88+
)
89+
);
90+
} else {
91+
return array('form' => $this->cargoForm);
92+
}
93+
}
94+
95+
/**
96+
* Set the CargoRepository
97+
*
98+
* @param Cargo\CargoRepositoryInterface $cargoRepository
99+
* @return void
100+
*/
101+
public function setCargoRepository(Cargo\CargoRepositoryInterface $cargoRepository)
102+
{
103+
$this->cargoRepository = $cargoRepository;
104+
}
105+
106+
/**
107+
* Set a cargo form.
108+
*
109+
* @param CargoForm $cargoForm
110+
* @return void
111+
*/
112+
public function setCargoForm(CargoForm $cargoForm)
113+
{
114+
$this->cargoForm = $cargoForm;
115+
}
116+
}

module/Application/src/Application/Controller/IndexController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<?php
2-
/**
3-
* Zend Framework (http://framework.zend.com/)
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
45
*
5-
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
6-
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7-
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
88
*/
9-
109
namespace Application\Controller;
1110

1211
use Zend\Mvc\Controller\AbstractActionController;
1312
use Zend\View\Model\ViewModel;
14-
13+
/**
14+
* MVC Controller for the introduction page
15+
*
16+
* @author Alexander Miertsch <[email protected]>
17+
*/
1518
class IndexController extends AbstractActionController
1619
{
1720
public function indexAction()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Form;
10+
11+
use Zend\Form\Form;
12+
use Zend\InputFilter\Input;
13+
use Zend\InputFilter\InputFilter;
14+
use Zend\Validator\StringLength;
15+
use Zend\Validator\Digits;
16+
/**
17+
* Form class to manage add and update of a Cargo.
18+
*
19+
* @author Alexander Miertsch <[email protected]>
20+
*/
21+
class CargoForm extends Form
22+
{
23+
public function __construct($name = null, $options = array())
24+
{
25+
parent::__construct('CargoForm', $options);
26+
27+
$this->add(array(
28+
'name' => 'size',
29+
'options' => array(
30+
'label' => 'Cargo Size',
31+
),
32+
'attributes' => array(
33+
'class' => 'form-control',
34+
),
35+
'type' => 'Text',
36+
37+
));
38+
39+
$this->add(array(
40+
'name' => 'trackingId',
41+
'type' => 'Hidden',
42+
));
43+
44+
$this->add(array(
45+
'name' => 'send',
46+
'type' => 'Submit',
47+
'attributes' => array(
48+
'value' => 'Submit',
49+
'class' => 'btn btn-success'
50+
),
51+
));
52+
}
53+
54+
public function getInputFilter()
55+
{
56+
if (is_null($this->filter)) {
57+
$sizeValidator = new Digits();
58+
$sizeInput = new Input('size');
59+
$sizeInput->setRequired(true);
60+
$sizeInput->getValidatorChain()
61+
->addValidator($sizeValidator);
62+
63+
$trackingIdValidator = new StringLength(13);
64+
$trackingIdInput = new Input('trackingId');
65+
$trackingIdInput->allowEmpty();
66+
$trackingIdInput->setRequired(false);
67+
$trackingIdInput->getValidatorChain()
68+
->addValidator($trackingIdValidator);
69+
70+
$filter = new InputFilter();
71+
$filter->add($sizeInput)->add($trackingIdInput);
72+
$this->filter = $filter;
73+
}
74+
75+
return $this->filter;
76+
}
77+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Form\Service;
10+
11+
use Zend\ServiceManager\FactoryInterface;
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
use Application\Form\CargoForm;
14+
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
15+
/**
16+
* CargoFormFactory
17+
*
18+
* @author Alexander Miertsch <[email protected]>
19+
*/
20+
class CargoFormFactory implements FactoryInterface
21+
{
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function createService(ServiceLocatorInterface $serviceLocator)
26+
{
27+
$cargoHydrator = new DoctrineObject(
28+
$serviceLocator->get('doctrine.entitymanager.orm_default'),
29+
'Application\Domain\Model\Cargo\Cargo'
30+
);
31+
32+
$cargoForm = new CargoForm();
33+
$cargoForm->setHydrator($cargoHydrator);
34+
35+
return $cargoForm;
36+
}
37+
}

0 commit comments

Comments
 (0)