Skip to content

Commit

Permalink
gjerokrsteski#23 - more on Application, reworked Environment & Param,…
Browse files Browse the repository at this point in the history
… small tweak in Util/Char/Clean and Util/Header/*
  • Loading branch information
garrettw committed Jun 20, 2017
1 parent 7cc273e commit ad96eaa
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 154 deletions.
136 changes: 53 additions & 83 deletions core/Pimf/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,60 @@ final class Application
*
* @return boolean|null
*/
public function __construct(Config $conf, array $server = [])
public function __construct(Config $conf, Environment $server)
{
$problems = [];
$this->env = $server;

$problems = [];
// nothing exceptionable should be happening in __construct()
try {

$environment = $conf['environment'];
$appname = $conf['app.name'];
// this belongs in run() somehow, but $conf isn't there
\date_default_timezone_set($conf['timezone']);

date_default_timezone_set($conf['timezone']);
// this one is a mess
$this->setupUtils($conf['bootstrap.local_temp_directory'], $conf->get('logging.storage', 'file'));

// this needs to be done before much user app code is run
$this->setupErrorHandling($conf['environment']);

$appPath = BASE_PATH . 'app/' . $conf['app.name'];

// $this->loadListeners();
if (file_exists($appPath . '/events.php')) {
include_once $appPath . '/events.php';
}

// $this->loadPdoDriver();
$dbConf = $conf[$conf['environment'] . '.db'];
if (is_array($dbConf) && $conf['environment'] != 'testing') {
$this->em = new EntityManager(Pdo\Factory::get($dbConf), $conf['app.name']);
}

// $this->loadRoutes();
if ($conf['app.routeable'] === true && file_exists($appPath . '/routes.php')) {
$this->router = new Router();

foreach ((array)(include $routes) as $route) {
$this->router->map($route);
}
}

$this->setupUtils($server, $conf['bootstrap.local_temp_directory'], $conf->get('logging.storage', 'file'));
$this->loadListeners(BASE_PATH . 'app/' . $appname . '/events.php');
$this->setupErrorHandling($environment);
$this->loadPdoDriver($environment, $conf[$environment . '.db'], $appname);
$this->loadRoutes(
$conf['app.routeable'],
BASE_PATH . 'app/' . $appname . '/routes.php'
);

} catch (\Throwable $throwable) {
$problems[] = $throwable->getMessage();
} catch (\Exception $exception) {
$problems[] = $exception->getMessage();
}

$this->reportIf($problems, PHP_VERSION);
// $this->reportIf();
if (version_compare($version, 5.3) == -1) {
$problems[] = 'You have PHP ' . PHP_VERSION . ' and you need 5.3 or higher!';
}

if (!empty($problems)) {
die(implode(PHP_EOL . PHP_EOL, $problems));
}
}

/**
Expand Down Expand Up @@ -171,15 +198,9 @@ function () use ($logger) {
* @param $tmpPath
* @param string $logging
*/
private function setupUtils(array $server, $tmpPath, $logging = 'file')
private function setupUtils($tmpPath, $logging = 'file')
{
self::$env = new Environment($server);
$envData = self::$env->data();

Logger::setup(
self::$env->getIp(),
$envData->get('PHP_SELF', $envData->get('SCRIPT_NAME'))
);
$envData = $this->env->data();

ResponseStatus::setup($envData->get('SERVER_PROTOCOL', 'HTTP/1.0'));

Expand All @@ -191,79 +212,28 @@ private function setupUtils(array $server, $tmpPath, $logging = 'file')
Uri::setup(self::$env->PATH_INFO, self::$env->REQUEST_URI);
Uuid::setup(self::$env->getIp(), self::$env->getHost());

$remoteIp = $this->env->getIp();
$script = $envData->get('PHP_SELF', $envData->get('SCRIPT_NAME'));

if ($logging === 'file') {
self::$logger = new Logger(
$this->logger = new Logger(
$remoteIp,
$script,
new Adapter\File($tmpPath, "pimf-logs.txt"),
new Adapter\File($tmpPath, "pimf-warnings.txt"),
new Adapter\File($tmpPath, "pimf-errors.txt")
);
} else {
self::$logger = new Logger(
$this->logger = new Logger(
$remoteIp,
$script,
new Adapter\Std(Adapter\Std::OUT),
new Adapter\Std(Adapter\Std::OUT),
new Adapter\Std(Adapter\Std::ERR)
);
}

self::$logger->init();
}

/**
* @param string $environment
* @param array $dbConf
* @param string $appName
*/
private function loadPdoDriver($environment, $dbConf, $appName)
{
if (is_array($dbConf) && $environment != 'testing') {
self::$em = new EntityManager(Pdo\Factory::get($dbConf), $appName);
}
}

/**
* @param boolean $routeable
* @param string $routes Path to routes definition file.
*/
private function loadRoutes($routeable, $routes)
{
if ($routeable === true && file_exists($routes)) {

self::$router = new Router();

foreach ((array)(include $routes) as $route) {

self::$router->map($route);

}
}
}

/**
* @param string $events Path to event listeners
*/
private function loadListeners($events)
{
if (file_exists($events)) {
include_once $events;
}
}

/**
* @param array $problems
* @param float $version
* @param bool $die
*
* @return array|void
*/
private function reportIf(array $problems, $version, $die = true)
{
if (version_compare($version, 5.3) == -1) {
$problems[] = 'You have PHP ' . $version . ' and you need 5.3 or higher!';
}

if (!empty($problems)) {
return ($die === true) ? die(implode(PHP_EOL . PHP_EOL, $problems)) : $problems;
}
$this->logger->init();
}

/**
Expand Down
6 changes: 1 addition & 5 deletions core/Pimf/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public function get($index, $default = null)
*/
public function offsetExists($offset)
{
if ($this->get($offset, null) !== null) {
return true;
}

return false;
return ($this->get($offset, null) !== null);
}

/**
Expand Down
23 changes: 11 additions & 12 deletions core/Pimf/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class Environment
/**
* @param array $server
*/
public function __construct(array $server)
public function __construct(Param $server)
{
$this->data = new Param($server);
$this->data = $server;
}

/**
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __get($key)
*/
public function isAjax()
{
return $this->X_REQUESTED_WITH === 'XMLHttpRequest';
return ($this->X_REQUESTED_WITH === 'XMLHttpRequest');
}

/**
Expand All @@ -101,7 +101,7 @@ public function isHttp()
*/
public function isHttps()
{
return $this->HTTPS === 'on';
return ($this->HTTPS === 'on');
}

/**
Expand All @@ -111,18 +111,17 @@ public function isHttps()
*/
public function getHost()
{
if ($this->HOST) {

if (strpos($this->HOST, ':') !== false) {
$hostParts = explode(':', $this->HOST);
if (!$this->HOST) {
return $this->SERVER_NAME;
}

return $hostParts[0];
}
if (strpos($this->HOST, ':') !== false) {
$hostParts = explode(':', $this->HOST);

return $this->HOST;
return $hostParts[0];
}

return $this->SERVER_NAME;
return $this->HOST;
}

/**
Expand Down
23 changes: 10 additions & 13 deletions core/Pimf/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,31 @@ class Logger
/**
* @var string
*/
private static $remoteIp;
private $remoteIp;

/**
* @var string
*/
private static $script;
private $script;

/**
* Logger constructor.
*
* @param string $remoteIp
* @param string $script
*/
public static function setup($remoteIp, $script)
{
self::$remoteIp = $remoteIp;
self::$script = $script;
}

/**
* Logger constructor.
* @param Contracts\Streamable $infoHandle
* @param Contracts\Streamable $warnHandle
* @param Contracts\Streamable $errorHandle
*/
public function __construct(
$remoteIp,
$script,
Contracts\Streamable $infoHandle,
Contracts\Streamable $warnHandle,
Contracts\Streamable $errorHandle)
{
Contracts\Streamable $errorHandle
) {
$this->remoteIp = $remoteIp;
$this->script = $script;
$this->infoHandle = $infoHandle->open();
$this->warnHandle = $warnHandle->open();
$this->errorHandle = $errorHandle->open();
Expand Down
Loading

0 comments on commit ad96eaa

Please sign in to comment.