Skip to content

Commit 4a0f0ac

Browse files
author
Codeliner
committed
Merge branch 'AddCargoFeature' into ChapterOneStory
2 parents 16fada2 + 94d9c5c commit 4a0f0ac

File tree

29 files changed

+1181
-32
lines changed

29 files changed

+1181
-32
lines changed

config/autoload/global.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
* You can use this file for overriding configuration values from modules, etc.
66
* You would place values in here that are agnostic to the environment and not
77
* sensitive to security.
8-
*
9-
* @NOTE: In practice, this file will typically be INCLUDED in your source
10-
* control, so do not include passwords or other sensitive information in this
11-
* file.
8+
*
129
*/
13-
1410
return array(
15-
// ...
11+
'service_manager' => array(
12+
'invokables' => array(
13+
'Doctrine\ORM\Mapping\UnderscoreNamingStrategy' => 'Doctrine\ORM\Mapping\UnderscoreNamingStrategy',
14+
),
15+
),
16+
'doctrine' => array(
17+
'configuration' => array(
18+
'orm_default' => array(
19+
'naming_strategy' => 'Doctrine\ORM\Mapping\UnderscoreNamingStrategy'
20+
),
21+
),
22+
),
1623
);

config/autoload/local.php.dist

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<?php
2-
/**
3-
* Local Configuration Override
4-
*
5-
* This configuration override file is for overriding environment-specific and
6-
* security-sensitive configuration information. Copy this file without the
7-
* .dist extension at the end and populate values as needed.
8-
*
9-
* @NOTE: This file is ignored from Git by default with the .gitignore included
10-
* in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
11-
* credentials from accidentally being committed into version control.
12-
*/
13-
14-
return array(
15-
);
2+
return array(
3+
'doctrine' => array(
4+
'connection' => array(
5+
'orm_default' => array(
6+
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
7+
'params' => array(
8+
'host' => 'localhost',
9+
'port' => '3306',
10+
'user' => 'root',
11+
'password' => '',
12+
'dbname' => 'cargo_sample',
13+
'charset' => 'utf8',
14+
'driverOptions' => array(
15+
1002 => "SET NAMES 'UTF8'"
16+
),
17+
)
18+
)
19+
),
20+
),
21+
);

module/Application/Module.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public function getAutoloaderConfig()
3232
'Zend\Loader\StandardAutoloader' => array(
3333
'namespaces' => array(
3434
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
35-
'Domain' => __DIR__ . '/src/Domain',
36-
'Infrastructure' => __DIR__ . 'src/Infrastructure'
3735
),
3836
),
3937
);

module/Application/config/module.config.php

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
77
* @license http://framework.zend.com/license/new-bsd New BSD License
88
*/
9-
109
return array(
1110
'router' => array(
1211
'routes' => array(
@@ -47,14 +46,33 @@
4746
'defaults' => array(
4847
),
4948
),
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+
),
5063
),
5164
),
5265
),
5366
),
5467
),
5568
'service_manager' => array(
5669
'factories' => array(
57-
'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+
},
5876
),
5977
'abstract_factories' => array(
6078
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
@@ -78,6 +96,18 @@
7896
'invokables' => array(
7997
'Application\Controller\Index' => 'Application\Controller\IndexController'
8098
),
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+
)
81111
),
82112
'view_manager' => array(
83113
'display_not_found_reason' => true,
@@ -95,11 +125,54 @@
95125
__DIR__ . '/../view',
96126
),
97127
),
128+
'doctrine' => array(
129+
'configuration' => array(
130+
'orm_default' => array(
131+
//Define custom doctrine types to map the ddd value objects
132+
'types' => array(
133+
'uid' => 'Application\Infrastructure\Persistence\Doctrine\Type\UID',
134+
'trackingid' => 'Application\Infrastructure\Persistence\Doctrine\Type\TrackingId',
135+
),
136+
),
137+
),
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+
)
152+
),
98153
'navigation' => array(
99154
'default' => array(
100155
'home' => array(
101156
'route' => 'home',
102157
'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+
)
103176
)
104177
)
105178
),
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()

0 commit comments

Comments
 (0)