Skip to content

Instantly share code, notes, and snippets.

@thepsion5
Created October 13, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepsion5/b28e5f9785084a85e11898d8e09e5602 to your computer and use it in GitHub Desktop.
Save thepsion5/b28e5f9785084a85e11898d8e09e5602 to your computer and use it in GitHub Desktop.
An example of how an external package containing pure domain logic can be cleanly integrated into different applications
<?php
//FILE: legacy-application/public/index.php
require_once __DIR__ . '/../vendor/autoload.php';
$config = require __DIR__ . '../config/settings.php';
$counties = require __DIR__ . '/../vendor/my-company/my-package/src/counties.php';
$app = \App\Application::createFromConfig($config, $counties);
$byEntity = isset($_GET['ByEntity']) ? $_GET['ByEntity'] : null;
$byCategory = isset($_GET['ByType']) ? $_GET['ByType'] : null;
$byCounty = isset($_GET['County']) ? $_GET['County'] : null;
echo $app->template('header'); ?>
<?php
if (!$app->resultsCanBeDisplayed()) {
echo $app->template('see_official_results');
} else {
if ($byEntity) {
$result = $app->results()->getResultsForEntity($byEntity, false);
echo $app->template('some-entity/single', compact('byEntity', 'result'));
} elseif ($byCategory) {
$results = $app->results()->getResultsForCategory($byCategory);
echo $app->template('some-entity/all', compact('results', 'byCategory'));
} elseif ($byCounty) {
$entities = $app->results()->getResultsForCounty($byCounty);
$totalByEntity = $app->results()->getTotalVotesByEntity($byCounty);
echo $app->template('some-entity/by_county', compact('byCounty', 'entities', 'totalsByEntity'));
} else {
echo $app->template('homepage');
}
}
<?php
//File: legacy-application/src/Application.php
namespace App;
use DateTime;
use Enr\Container;
use Enr\Domain\SomeEntity\EntityResultsRepository;
class Application
{
/* properties, other methods omitted for brevity */
public function __construct(Container $enrContainer, DateTime $displayDisabledAfter, $templatePath, $showTestingHeader = false)
{
$this->enrContainer = $enrContainer;
$this->displayDisabledAfter = $displayDisabledAfter;
$this->setTemplatePath($templatePath);
$this->showTestingHeader = $showTestingHeader;
}
/**
* @return EntityResultsRepository
*/
public function results()
{
return $this->enrContainer->results();
}
/**
* Creates a new Application instance from a standard configuration array
*
* @param array $config
* @param array $counties
* @return Application
*/
public static function createFromConfig(array $config, array $counties)
{
$app = new static(
new Container($config['enr_config'], $counties),
DateTime::createFromFormat('Y-m-d', $config['display_disabled_after']),
$config['template_path'],
$config['show_testing_header']
);
if ($config['debug']) {
$app->registerErrorHandler();
}
return $app;
}
}
<?php
namespace Enr;
class Container
{
/* properties, other methods omitted for brevity */
public function __construct(array $config, array $counties)
{
$this->config = $config;
$this->counties = $counties;
}
/**
* @return \Enr\Domain\SomeEntity\EntityResultsRepository
*/
public function results()
{
return $this->getRepoFactory()->makeResultsRepo();
}
}
<?php
//FILE: slim-3-app/config/dependencies.php
use Enr\Container as EnrContainer;
use Enr\SomeEntity\EntityResultsRepository;
$container = $app->getContainer();
$container[ EnrContainer::class ] = function($c) {
$config = $c->get('enr_config');
$counties = require __DIR__ . '/../vendor/my-company/my-package/src/counties.php';
return new EnrContainer($config, $counties);
};
//Entity Results Repository
$container[ EntityResultsRepository::class ] = function($c) {
return $c->get(EnrContainer::class)->results();
};
<?php
// FILE: slim-3-app/public/index.php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../config/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../config/dependencies.php';
// Register middleware
require __DIR__ . '/../config/middleware.php';
// Register routes
require __DIR__ . '/../config/routes.php';
// Run app
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment