Permalink
Browse files

Version24

To show off the benefits of decoupling domain from the framework I
wanted to show an example of how the domain could be used outside the
standard browser, so I thought it'd be cool to set up a quick CLI
example. Symfony's Command module makes it trivial to add CLI programs,
and you can see from this commit how exactly it works. Essentially it's
the same deal as controllers. Request in -> Create command -> Pass off
to Handler.
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent dcb675d commit 4fdc5ed94368ef1656ea80e6fde634a58020cc04
@@ -50,7 +50,7 @@ public static function getHouse($currentLocation = null)
$kitchen->setDimensions(Dimensions::fromWidthAndHeight(400, 300));
$kitchen->setExits([$hallway, $diningRoom]);
$kitchen->setDimensions(Dimensions::fromWidthAndHeight(300, 200));
$diningRoom->setDimensions(Dimensions::fromWidthAndHeight(300, 200));
$diningRoom->setExits([$hallway, $kitchen, $livingRoom]);
$bathroom->setDimensions(Dimensions::fromWidthAndHeight(400, 100));
@@ -74,4 +74,12 @@ public function getExits()
{
return $this->exits;
}
/**
* @return string
*/
public function __toString()
{
return $this->getName();
}
}
@@ -0,0 +1,106 @@
<?php
namespace Jenko\HouseBundle\Command;
use Jenko\House\Command\EnterRoomCommand;
use Jenko\House\Factory\HomeAloneHouseFactory;
use Jenko\House\Handler\EnterRoomHandler;
use Jenko\House\House;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
class NavigateHouseCommand extends ContainerAwareCommand
{
/**
* @var House $house
*/
private $house;
/**
* @var EnterRoomHandler $enterRoomHandler
*/
private $enterRoomHandler;
public function __construct()
{
// Start outside
$this->house = HomeAloneHouseFactory::getHouse('front-garden');
parent::__construct();
}
protected function configure()
{
$this
->setName('jenko:house:navigate')
->setDescription('Navigate the house')
->addOption('location', null, InputOption::VALUE_OPTIONAL, 'If set, the house will start from this room')
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->enterRoomHandler = $this->getContainer()->get('jenko.house.handlers.enter_room_handler');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$room = $input->getOption('location');
if (null === $room) {
$room = $this->house->whereAmI();
}
$command = new EnterRoomCommand();
$command->room = $room;
$house = $this->enterRoomHandler->handle($command);
$output->writeln('You are in: ' . $room);
$info = $house->whereAmI()->getInformation();
$table = $this->getHelper('table');
$table
->setHeaders(array('dimensions', 'exits'))
->setRows(array(
array($info['dimensions'], implode(", ", $info['exits'])),
))
;
$table->render($output);
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Where would you like to go?',
$info['exits'],
0
);
$question->setErrorMessage('Location %s is invalid.');
$location = $helper->ask($input, $output, $question);
$nextCommandName = 'jenko:house:navigate';
$args['--location'] = $location;
$nextCommand = $this->getApplication()->find($nextCommandName);
$args['command'] = $nextCommand;
$input = new ArrayInput($args);
$nextCommand->run($input, $output);
}
}

0 comments on commit 4fdc5ed

Please sign in to comment.