From 56d6ac12c802660693e9f284f833bfed012840f0 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 14 May 2014 11:58:49 -0400 Subject: [PATCH] Remove some redundancies --- Sleepy/Core/Input.php | 62 ++++++++++----------------------------- application/bootstrap.php | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 application/bootstrap.php diff --git a/Sleepy/Core/Input.php b/Sleepy/Core/Input.php index 9992839..8f340b1 100755 --- a/Sleepy/Core/Input.php +++ b/Sleepy/Core/Input.php @@ -87,10 +87,8 @@ class Input { /** * Instantiates the class - * - * @param array $config */ - public function __construct($config = []) + public function __construct() { // Type of HTTP request $this->verb = \strtolower($_SERVER['REQUEST_METHOD']); @@ -148,17 +146,11 @@ class Input { $index = $args[0]; $filter = $args[1]; - if (isset($this->var_map[$name])) + if (isset($this->var_map[$name]) || isset($this->$name)) { - // Get a superglobal ($_VAR) value - return $this->get_superglobal_var($name, $index, $filter); + // Get an input variable value + return $this->get_var($name, $index, $filter); } - else if(isset($this->$name)) // @codeCoverageIgnoreStart - { - // Get a input variable not in a superglobal (eg. PUT/DELETE) - return $this->get_request_var($name, $index, $filter); - } - // @codeCoverageIgnoreEnd // What kind of request are you trying to make?! throw new \DomainException('Invalid input array.'); @@ -357,57 +349,33 @@ class Input { return $output; } - + /** - * Get input var(s) from non-defined superglobal - * - * @codeCoverageIgnore - * @param string $type - input array + * Get superglobal or generated input array value + * @param string $type - HTTP verb * @param string $index - variable in the input array * @param int $filter - PHP filter_var flag * @return mixed */ - protected function get_request_var($type, $index=NULL, $filter=FILTER_SANITIZE_STRING) + protected function get_var($type, $index=NULL, $filter=\FILTER_SANITIZE_STRING) { - // If index is null, return the whole array - if ($index === NULL) - { - return ($filter !== NULL) ? \filter_var_array($this->$type, $filter) : $this->$type; - } - - // Prevent errors for non-existant variables - if ( ! isset($this->$type[$index])) - { - return NULL; - } - - return ($filter !== NULL) ? \filter_var($this->$type[$index], $filter) : $this->$type[$index]; - } - - /** - * Get index from superglobal - * - * @param string $type - superglobal - * @param string $index - variable in the superglobal - * @param int $filter - PHP filter_var flag - * @return mixed - */ - protected function get_superglobal_var($type, $index=NULL, $filter=FILTER_SANITIZE_STRING) - { - $var =& $this->var_map[$type]; - + // Determine if superglobal or generated + $var = array_key_exists($type, $this->var_map) + ? $this->var_map[$type] + : $this->$type; + // Return the whole array if the index is null if ($index === NULL) { return ($filter !== NULL) ? \filter_var_array($var, $filter) : $var; } - + // Prevent errors for non-existant variables if ( ! isset($var[$index])) { return NULL; } - + return ($filter !== NULL) ? \filter_var($var[$index], $filter) : $var[$index]; } diff --git a/application/bootstrap.php b/application/bootstrap.php new file mode 100644 index 0000000..d7d585f --- /dev/null +++ b/application/bootstrap.php @@ -0,0 +1,51 @@ +set('config', $di->newInstance('Sleepy\Core\Config')); +$di->set('router', $di->newInstance('Aura\Router\RouterFactory')->newInstance()); +$di->set('input', $di->newInstance('Sleepy\Core\Input')); + +$di->params['Sleepy\Core\Output'] = [ + 'config' => $di->get('config'), + 'input' => $di->get('input') +]; +$di->set('output', function () use ($di) { + return $di->newInstance('Sleepy\Core\Output'); +}); + +// Set the output data +$browser = get_browser(); +$browser->browser_name_regex = \utf8_encode($browser->browser_name_regex); +$i = $di->get('input'); +$di->get('output')->set_data(['json','yaml','xml','html'], [ + '$_SERVER' => $i->server(), + '$_GET' => $i->get(), + '$_POST' => $i->post(), + '$_PUT' => $i->put(), + '$_DELETE' => $i->delete(), + '$_ENV' => $i->env(), + '$_COOKIE' => $i->cookie(), + 'browser' => $browser, + 'raw headers' => $i->header(), + 'parsed headers' => $i->header_array() +]); + +// End of bootstrap.php \ No newline at end of file