Skip to content

Commit

Permalink
Version20
Browse files Browse the repository at this point in the history
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 20, 2015
1 parent 0952bb4 commit b8ebbea
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 54 deletions.
1 change: 0 additions & 1 deletion app/AppKernel.php
Expand Up @@ -16,7 +16,6 @@ public function registerBundles()
new Symfony\Bundle\AsseticBundle\AsseticBundle(), new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Tabbi89\CommanderBundle\Tabbi89CommanderBundle(),


// House Bundles // House Bundles
new Jenko\HouseBundle\JenkoHouseBundle(), new Jenko\HouseBundle\JenkoHouseBundle(),
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -18,8 +18,7 @@
"sensio/distribution-bundle": "~3.0", "sensio/distribution-bundle": "~3.0",
"sensio/framework-extra-bundle": "~3.0", "sensio/framework-extra-bundle": "~3.0",
"incenteev/composer-parameter-handler": "~2.0", "incenteev/composer-parameter-handler": "~2.0",
"phpspec/phpspec": "~2.0@RC", "phpspec/phpspec": "~2.0@RC"
"tabbi89/commander-bundle": "~1.0"
}, },
"require-dev": { "require-dev": {
"sensio/generator-bundle": "~2.3", "sensio/generator-bundle": "~2.3",
Expand Down
93 changes: 47 additions & 46 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -2,7 +2,7 @@


namespace Jenko\House\Command; namespace Jenko\House\Command;


final class ExitRoom final class EnterRoomCommand
{ {
public $room; public $room;
} }
Expand Up @@ -2,7 +2,7 @@


namespace Jenko\House\Command; namespace Jenko\House\Command;


final class EnterRoom final class ExitRoomCommand
{ {
public $room; public $room;
} }
2 changes: 1 addition & 1 deletion src/Jenko/House/Handler/EnterRoomHandler.php
Expand Up @@ -19,6 +19,6 @@ public function __construct()
*/ */
public function handle($command) public function handle($command)
{ {
$this->house->enterRoom($command->room); return $this->house->enterRoom($command->room);
} }
} }
2 changes: 1 addition & 1 deletion src/Jenko/House/Handler/ExitRoomHandler.php
Expand Up @@ -19,6 +19,6 @@ public function __construct()
*/ */
public function handle($command) public function handle($command)
{ {
$this->house->exitToRoom($command->room); return $this->house->exitToRoom($command->room);
} }
} }
6 changes: 5 additions & 1 deletion src/Jenko/House/House.php
Expand Up @@ -64,6 +64,7 @@ public function whereWasI()


/** /**
* @param Room|string $room * @param Room|string $room
* @return $this
* @throws LocationDoesNotExistException * @throws LocationDoesNotExistException
*/ */
public function enterRoom($room) public function enterRoom($room)
Expand All @@ -78,6 +79,8 @@ public function enterRoom($room)


$this->previousLocation = $this->currentLocation; $this->previousLocation = $this->currentLocation;
$this->currentLocation = $room; $this->currentLocation = $room;

return $this;
} }


/** /**
Expand Down Expand Up @@ -113,9 +116,10 @@ private function containsLocation(Location $location)


/** /**
* @param Location|string $room * @param Location|string $room
* @return House
*/ */
public function exitToRoom($room) public function exitToRoom($room)
{ {
$this->enterRoom($room); return $this->enterRoom($room);
} }
} }
46 changes: 46 additions & 0 deletions src/Jenko/HouseBundle/Controller/EnterRoomController.php
@@ -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()]
);
}
}
23 changes: 23 additions & 0 deletions src/Jenko/HouseBundle/Controller/OutsideController.php
@@ -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');
}
}
20 changes: 20 additions & 0 deletions src/Jenko/HouseBundle/DependencyInjection/JenkoHouseExtension.php
@@ -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');
}
}
10 changes: 10 additions & 0 deletions src/Jenko/HouseBundle/JenkoHouseBundle.php
@@ -0,0 +1,10 @@
<?php

namespace Jenko\HouseBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class JenkoHouseBundle extends Bundle
{

}
11 changes: 11 additions & 0 deletions src/Jenko/HouseBundle/Resources/config/routing.yml
@@ -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
14 changes: 14 additions & 0 deletions src/Jenko/HouseBundle/Resources/config/services.yml
@@ -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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/Jenko/HouseBundle/Resources/views/outside.html.twig
@@ -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>
13 changes: 13 additions & 0 deletions src/Jenko/HouseBundle/Resources/views/room.html.twig
@@ -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.