Permalink
Browse files

Version20

Adding Symfony bundle and some basic infrastructure.

This is where version0 should have been.

Few things to point out here. Firstly, the lightweight controllers. They
do little more than take the request, set up the command and pass it off
to the handler.

When I first started this, I had seen a few tech talks and was all about
decouple from the framework, controllers as services etc. I agree
massively with decouple from the framework with regards the domain and
even the ports layer (commands/handlers) but I've come a little bit full
circle in the controllers as services and using DI to completely
decouple your controllers. I know think, it doesn't really matter. So
long as your controllers are really light, it doesn't really matter if
they're coupled to shit, because essentially they don't matter. They are
merely passing messages from framework to domain. The second point to
note is that you can see from the images and twig templates how advaned
my UI is :).
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent 0952bb4 commit b8ebbea1bf200e399661f252acc4aee9ff7d2da9
View
@@ -16,7 +16,6 @@ public function registerBundles()
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Tabbi89\CommanderBundle\Tabbi89CommanderBundle(),
// House Bundles
new Jenko\HouseBundle\JenkoHouseBundle(),
View
@@ -18,8 +18,7 @@
"sensio/distribution-bundle": "~3.0",
"sensio/framework-extra-bundle": "~3.0",
"incenteev/composer-parameter-handler": "~2.0",
"phpspec/phpspec": "~2.0@RC",
"tabbi89/commander-bundle": "~1.0"
"phpspec/phpspec": "~2.0@RC"
},
"require-dev": {
"sensio/generator-bundle": "~2.3",
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -2,7 +2,7 @@
namespace Jenko\House\Command;
final class ExitRoom
final class EnterRoomCommand
{
public $room;
}
@@ -2,7 +2,7 @@
namespace Jenko\House\Command;
final class EnterRoom
final class ExitRoomCommand
{
public $room;
}
@@ -19,6 +19,6 @@ public function __construct()
*/
public function handle($command)
{
$this->house->enterRoom($command->room);
return $this->house->enterRoom($command->room);
}
}
@@ -19,6 +19,6 @@ public function __construct()
*/
public function handle($command)
{
$this->house->exitToRoom($command->room);
return $this->house->exitToRoom($command->room);
}
}
@@ -64,6 +64,7 @@ public function whereWasI()
/**
* @param Room|string $room
* @return $this
* @throws LocationDoesNotExistException
*/
public function enterRoom($room)
@@ -78,6 +79,8 @@ public function enterRoom($room)
$this->previousLocation = $this->currentLocation;
$this->currentLocation = $room;
return $this;
}
/**
@@ -113,9 +116,10 @@ private function containsLocation(Location $location)
/**
* @param Location|string $room
* @return House
*/
public function exitToRoom($room)
{
$this->enterRoom($room);
return $this->enterRoom($room);
}
}
@@ -0,0 +1,46 @@
<?php
namespace Jenko\HouseBundle\Controller;
use Jenko\House\Command\EnterRoomCommand;
use Jenko\House\Handler\EnterRoomHandler;
use Jenko\House\House;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EnterRoomController
{
/**
* @var EngineInterface
*/
private $templating;
/**
* @param EngineInterface $templating
*/
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* @param Request $request
*
* @return Response
*/
public function enterAction(Request $request)
{
$command = new EnterRoomCommand();
$command->room = $request->get('location');
$handler = new EnterRoomHandler();
/** @var House $house */
$house = $handler->handle($command);
return $this->templating->renderResponse(
'JenkoHouseBundle::room.html.twig',
['currentRoom' => $house->whereAmI(), 'previousRoom' => $house->whereWasI()]
);
}
}
@@ -0,0 +1,23 @@
<?php
namespace Jenko\HouseBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class OutsideController
{
/**
* @var EngineInterface
*/
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
public function outsideAction()
{
return $this->templating->renderResponse('JenkoHouseBundle::outside.html.twig');
}
}
@@ -0,0 +1,20 @@
<?php
namespace Jenko\HouseBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class JenkoHouseExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
@@ -0,0 +1,10 @@
<?php
namespace Jenko\HouseBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class JenkoHouseBundle extends Bundle
{
}
@@ -0,0 +1,11 @@
jenko_house_outside:
path: /
defaults: { _controller: jenko.house.controllers.outside:outsideAction }
requirements:
_method: GET
jenko_house_enter_room:
path: /location/{location}
defaults: { _controller: jenko.house.controllers.enter_room:enterAction }
requirements:
_method: GET
@@ -0,0 +1,14 @@
parameters:
jenko.house.controllers.outside.class: Jenko\HouseBundle\Controller\OutsideController
jenko.house.controllers.enter_room.class: Jenko\HouseBundle\Controller\EnterRoomController
services:
jenko.house.controllers.outside:
class: %jenko.house.controllers.outside.class%
arguments:
- @templating
jenko.house.controllers.enter_room:
class: %jenko.house.controllers.enter_room.class%
arguments:
- @templating
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
<img src="{{ asset('bundles/jenkohouse/img/front-garden.jpg') }}" />
<p><a href="{{ path('jenko_house_enter_room', {location: 'hallway'}) }}">Enter front door</a></p>
@@ -0,0 +1,13 @@
<img src="{{ asset('bundles/jenkohouse/img/' ~ currentRoom.name ~ '.jpg') }}" />
<p>Room size: {{ currentRoom.dimensions }}</p>
{% for room in currentRoom.exits %}
<p><a href="{{ path('jenko_house_enter_room', {location: room.name}) }}">Go to {{ room.name }}</a></p>
{% endfor %}
{% if previousRoom is not null %}
<p><a href="{{ path('jenko_house_enter_room', {location: previousRoom.name}) }}">Go back to {{ previousRoom.name }}</a></p>
{% else %}
<p><a href="{{ path('jenko_house_outside') }}">Go outside</a></p>
{% endif %}

0 comments on commit b8ebbea

Please sign in to comment.