Skip to content

Commit

Permalink
Add a service container.
Browse files Browse the repository at this point in the history
Add the various pieces that will be needed for the watch functions list
to the service container. Also add a stub for the watch controller as
well.

Using pimple will allow the controller objects and all their
dependencies to be separated from the global soup that Slim naturally
encourages. My goal is to have functional tests for all the various
controller methods.
  • Loading branch information
markstory committed Aug 15, 2013
1 parent 85a707b commit 0041530
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 32 deletions.
81 changes: 81 additions & 0 deletions src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
use Slim\Slim;
use Slim\Views\Twig;
use Slim\Middleware\SessionCookie;

class Xhgui_ServiceContainer extends Pimple
{
protected static $_instance;

public static function instance()
{
if (empty(static::$_instance)) {
static::$_instance = new self();
}
return static::$_instance;
}

public function __construct()
{
$this['config'] = include XHGUI_ROOT_DIR . '/config/config.php';
$this->_slimApp();
$this->_services();
$this->_controllers();
}

// Create the Slim app.
protected function _slimApp()
{
$this['app'] = $this->share(function ($c) {
$app = new Slim($c['config']);

// Enable cookie based sessions
$app->add(new SessionCookie(array(
'httponly' => true,
)));

// Configure Twig view for slim
$view = new Twig();
$view->parserOptions = array(
'charset' => 'utf-8',
'cache' => XHGUI_ROOT_DIR . '/cache',
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$view->parserExtensions = array(
new Xhgui_Twig_Extension($app)
);
$app->view($view);

return $app;
});
}

/**
* Add common service objects to the container.
*/
protected function _services()
{
$this['db'] = $this->share(function ($c) {
$config = $c['config'];
$mongo = new MongoClient($config['db.host']);
return $mongo->{$config['db.db']};
});

$this['watchFunctions'] = function ($c) {
return new Xhgui_WatchFunctions($c['db']);
};
}

/**
* Add controllers to the DI container.
*/
protected function _controllers()
{
$this['watchController'] = $this->share(function ($c) {
return new Xhgui_Controller_Watch($c['app'], $c['watchFunctions']);
});
}

}
4 changes: 2 additions & 2 deletions src/Xhgui/WatchFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class Xhgui_WatchFunctions
{
protected $_collection;

public function __construct(MongoCollection $collection)
public function __construct(MongoDb $db)
{
$this->_collection = $collection;
$this->_collection = $db->watches;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/ProfilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public function setUp()
{
Xhgui_Config::load(XHGUI_ROOT_DIR . '/config/config.php');
$db = Xhgui_Db::connect();
$this->profiles = new Xhgui_Profiles($db->test_results);
$this->profiles = new Xhgui_Profiles($db->results);
$this->profiles->truncate();
$this->_loadFixture('tests/fixtures/results.json');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/WatchFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class WatchFunctionsTest extends PHPUnit_Framework_TestCase
public function setUp()
{
parent::setUp();
$db = Xhgui_Db::connect();
$this->watch = new Xhgui_WatchFunctions($db->test_watch);
$di = Xhgui_ServiceContainer::instance();
$this->watch = $di['watchFunctions'];
$this->watch->truncate();
}

Expand Down
10 changes: 8 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
require dirname(__DIR__) . '/src/bootstrap.php';

$di = Xhgui_ServiceContainer::instance();

// Use a test database.
$dbname = Xhgui_Config::read('db.db');
Xhgui_Config::write('db.db', 'test_' . $dbname);
$config = $di['config'];
$config['db.db'] = 'test_' . $config['db.db'];
$di['config'] = $config;

// Clean up globals.
unset($di, $config);
28 changes: 3 additions & 25 deletions webroot/index.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
<?php
require dirname(__DIR__) . '/src/bootstrap.php';

use Slim\Slim;
use Slim\Views\Twig;
use Slim\Middleware\SessionCookie;
$di = new Xhgui_ServiceContainer();

$config = include XHGUI_ROOT_DIR . '/config/config.php';
$app = new Slim($config);

// Configure Twig view for slim
$view = new Twig();
$view->parserOptions = array(
'charset' => 'utf-8',
'cache' => XHGUI_ROOT_DIR . '/cache',
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$view->parserExtensions = array(
new Xhgui_Twig_Extension($app)
);
$app->view($view);

// Enable cookie based sessions
$app->add(new SessionCookie(array(
'httponly' => true,
)));
$app = $di['app'];

require XHGUI_ROOT_DIR . '/src/app/hooks.php';
require XHGUI_ROOT_DIR . '/src/app/controllers/runs.php';
require XHGUI_ROOT_DIR . '/src/app/controllers/custom.php';
require XHGUI_ROOT_DIR . '/src/app/controllers/watch.php';
require XHGUI_ROOT_DIR . '/src/app/controllers/error.php';
require XHGUI_ROOT_DIR . '/src/app/routes.php';

$app->run();

0 comments on commit 0041530

Please sign in to comment.