From 6354a816db6d30c18a2d841b7ab32b49ba03acd4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 26 Aug 2016 17:21:50 -0400 Subject: [PATCH] Really ugly progress commit --- .editorconfig | 20 ++++ RoboFile.php | 38 ++++++- build/CodeIgniter-for-PHP_CodeSniffer | 1 + build/phpcs.xml | 53 +++++++++ build/phpdox.xml | 7 +- build/phpunit.xml | 2 +- build/update_header_comments.php | 2 +- composer.json | 24 +++-- phpunit.xml | 2 +- src/{Aviat => }/Ion/ArrayWrapper.php | 0 src/{Aviat => }/Ion/Cache/CacheInterface.php | 12 +-- src/{Aviat => }/Ion/Cache/CacheManager.php | 14 +-- .../Ion/Cache/Driver/DriverInterface.php | 2 +- .../Ion/Cache/Driver/DriverTrait.php | 0 .../Ion/Cache/Driver/NullDriver.php | 2 +- .../Ion/Cache/Driver/RedisDriver.php | 4 +- .../Ion/Cache/Driver/SQLDriver.php | 2 +- src/{Aviat => }/Ion/Config.php | 2 +- src/{Aviat => }/Ion/ConfigInterface.php | 2 +- src/{Aviat => }/Ion/Di/Container.php | 4 +- src/{Aviat => }/Ion/Di/ContainerAware.php | 0 .../Ion/Di/ContainerAwareInterface.php | 0 src/{Aviat => }/Ion/Di/ContainerInterface.php | 4 +- .../Ion/Di/Exception/ContainerException.php | 0 .../Ion/Di/Exception/NotFoundException.php | 0 src/{Aviat => }/Ion/Enum.php | 2 +- .../Ion/Exception/ConfigException.php | 0 src/Ion/Exception/DoubleRenderException.php | 31 ++++++ src/{Aviat => }/Ion/Friend.php | 4 +- src/{Aviat => }/Ion/Json.php | 24 ++--- src/{Aviat => }/Ion/JsonException.php | 0 src/{Aviat => }/Ion/Model.php | 42 ++++---- src/{Aviat => }/Ion/Model/DB.php | 102 +++++++++--------- src/{Aviat => }/Ion/StaticInstance.php | 4 +- src/{Aviat => }/Ion/StringWrapper.php | 0 .../Ion/Transformer/AbstractTransformer.php | 0 .../Ion/Transformer/TransformerInterface.php | 0 src/{Aviat => }/Ion/Type/ArrayType.php | 12 +-- src/{Aviat => }/Ion/Type/StringType.php | 0 src/{Aviat => }/Ion/View.php | 58 +++------- src/{Aviat => }/Ion/View/HtmlView.php | 16 +-- src/{Aviat => }/Ion/View/HttpView.php | 28 +++-- src/{Aviat => }/Ion/View/JsonView.php | 5 +- src/Ion/ViewInterface.php | 62 +++++++++++ tests/Ion/Cache/CacheManagerTest.php | 6 +- .../Exception/DoubleRenderExceptionTest.php | 14 +++ tests/Ion/JsonTest.php | 4 +- tests/Ion/View/HtmlViewTest.php | 4 +- tests/Ion/View/HttpViewTest.php | 33 +++++- tests/Ion/View/JsonViewTest.php | 10 +- tests/bootstrap.php | 19 +--- tests/mocks.php | 59 ++++++++-- 52 files changed, 497 insertions(+), 239 deletions(-) create mode 100644 .editorconfig create mode 160000 build/CodeIgniter-for-PHP_CodeSniffer create mode 100644 build/phpcs.xml rename src/{Aviat => }/Ion/ArrayWrapper.php (100%) rename src/{Aviat => }/Ion/Cache/CacheInterface.php (67%) rename src/{Aviat => }/Ion/Cache/CacheManager.php (85%) rename src/{Aviat => }/Ion/Cache/Driver/DriverInterface.php (96%) rename src/{Aviat => }/Ion/Cache/Driver/DriverTrait.php (100%) rename src/{Aviat => }/Ion/Cache/Driver/NullDriver.php (97%) rename src/{Aviat => }/Ion/Cache/Driver/RedisDriver.php (97%) rename src/{Aviat => }/Ion/Cache/Driver/SQLDriver.php (98%) rename src/{Aviat => }/Ion/Config.php (97%) rename src/{Aviat => }/Ion/ConfigInterface.php (94%) rename src/{Aviat => }/Ion/Di/Container.php (97%) rename src/{Aviat => }/Ion/Di/ContainerAware.php (100%) rename src/{Aviat => }/Ion/Di/ContainerAwareInterface.php (100%) rename src/{Aviat => }/Ion/Di/ContainerInterface.php (93%) rename src/{Aviat => }/Ion/Di/Exception/ContainerException.php (100%) rename src/{Aviat => }/Ion/Di/Exception/NotFoundException.php (100%) rename src/{Aviat => }/Ion/Enum.php (97%) rename src/{Aviat => }/Ion/Exception/ConfigException.php (100%) create mode 100644 src/Ion/Exception/DoubleRenderException.php rename src/{Aviat => }/Ion/Friend.php (97%) rename src/{Aviat => }/Ion/Json.php (86%) rename src/{Aviat => }/Ion/JsonException.php (100%) rename src/{Aviat => }/Ion/Model.php (87%) rename src/{Aviat => }/Ion/Model/DB.php (92%) rename src/{Aviat => }/Ion/StaticInstance.php (96%) rename src/{Aviat => }/Ion/StringWrapper.php (100%) rename src/{Aviat => }/Ion/Transformer/AbstractTransformer.php (100%) rename src/{Aviat => }/Ion/Transformer/TransformerInterface.php (100%) rename src/{Aviat => }/Ion/Type/ArrayType.php (97%) rename src/{Aviat => }/Ion/Type/StringType.php (100%) rename src/{Aviat => }/Ion/View.php (64%) rename src/{Aviat => }/Ion/View/HtmlView.php (97%) rename src/{Aviat => }/Ion/View/HttpView.php (80%) rename src/{Aviat => }/Ion/View/JsonView.php (88%) create mode 100644 src/Ion/ViewInterface.php create mode 100644 tests/Ion/Exception/DoubleRenderExceptionTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b909682 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = false +charset = utf-8 +indent_style = tab +trim_trailing_whitespace = true + +[*.{cpp,c,h,hpp,cxx}] +insert_final_newline = true + +# Yaml files +[*.{yml,yaml}] +indent_style = space +indent_size = 4 \ No newline at end of file diff --git a/RoboFile.php b/RoboFile.php index 7dd95a8..3690d95 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -20,8 +20,13 @@ if ( ! function_exists('glob_recursive')) * * @see http://robo.li/ */ -class RoboFile extends \Robo\Tasks -{ +class RoboFile extends \Robo\Tasks { + + /** + * Directories used by analysis tools + * + * @var array + */ protected $taskDirs = [ 'build/api', 'build/coverage', @@ -41,6 +46,7 @@ class RoboFile extends \Robo\Tasks $this->phploc(TRUE); $this->dependencyReport(); $this->phpcpdReport(); + $this->phpcsReport(); } /** @@ -135,6 +141,20 @@ class RoboFile extends \Robo\Tasks $this->_run($cmd_parts); } + /** + * Run the phpcs tool + */ + public function phpcs() + { + $cmd_parts = [ + 'vendor/bin/phpcs', + '--standard=./build/phpcs.xml', + '--report=summary' + ]; + + $this->_run($cmd_parts); + } + /** * Run the phploc tool * @@ -145,6 +165,7 @@ class RoboFile extends \Robo\Tasks // Command for generating reports $report_cmd_parts = [ 'vendor/bin/phploc', + '--count-tests', '--log-csv=build/logs/phploc.csv', '--log-xml=build/logs/phploc.xml', 'src', @@ -263,6 +284,19 @@ class RoboFile extends \Robo\Tasks $this->_run($cmd_parts); } + /** + * Generate code style report + */ + protected function phpcsReport() + { + $cmd_parts = [ + 'vendor/bin/phpcs', + '--standard=./build/phpcs.xml', + '--report-xml=./build/logs/phpcs.xml' + ]; + $this->_run($cmd_parts); + } + /** * Short cut for joining an array of command arguments * and then running it diff --git a/build/CodeIgniter-for-PHP_CodeSniffer b/build/CodeIgniter-for-PHP_CodeSniffer new file mode 160000 index 0000000..b501f01 --- /dev/null +++ b/build/CodeIgniter-for-PHP_CodeSniffer @@ -0,0 +1 @@ +Subproject commit b501f01403216a917754e3f76ba7022d4ad37a4e diff --git a/build/phpcs.xml b/build/phpcs.xml new file mode 100644 index 0000000..419771f --- /dev/null +++ b/build/phpcs.xml @@ -0,0 +1,53 @@ + + + A variation of the CodeIgniter standard + + ../src/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build/phpdox.xml b/build/phpdox.xml index 73d9693..7ec37a2 100644 --- a/build/phpdox.xml +++ b/build/phpdox.xml @@ -48,6 +48,7 @@ + @@ -84,11 +85,9 @@ - - + diff --git a/build/phpunit.xml b/build/phpunit.xml index a2c56dc..9677773 100644 --- a/build/phpunit.xml +++ b/build/phpunit.xml @@ -8,7 +8,7 @@ > - ../src/Aviat/Ion + ../src/Ion diff --git a/build/update_header_comments.php b/build/update_header_comments.php index a6c0114..402a972 100644 --- a/build/update_header_comments.php +++ b/build/update_header_comments.php @@ -1,7 +1,7 @@ - src/Aviat/Ion + src/Ion diff --git a/src/Aviat/Ion/ArrayWrapper.php b/src/Ion/ArrayWrapper.php similarity index 100% rename from src/Aviat/Ion/ArrayWrapper.php rename to src/Ion/ArrayWrapper.php diff --git a/src/Aviat/Ion/Cache/CacheInterface.php b/src/Ion/Cache/CacheInterface.php similarity index 67% rename from src/Aviat/Ion/Cache/CacheInterface.php rename to src/Ion/Cache/CacheInterface.php index 4a4413b..6658a2b 100644 --- a/src/Aviat/Ion/Cache/CacheInterface.php +++ b/src/Ion/Cache/CacheInterface.php @@ -21,9 +21,9 @@ interface CacheInterface { * Retrieve a cached value if it exists, otherwise, get the value * from the passed arguments * - * @param object $object - object to retrieve fresh value from - * @param string $method - method name to call - * @param [array] $args - the arguments to pass to the retrieval method + * @param object $object - object to retrieve fresh value from + * @param string $method - method name to call + * @param [array] $args - the arguments to pass to the retrieval method * @return mixed - the cached or fresh data */ public function get($object, $method, array $args=[]); @@ -31,9 +31,9 @@ interface CacheInterface { /** * Retrieve a fresh value, and update the cache * - * @param object $object - object to retrieve fresh value from - * @param string $method - method name to call - * @param [array] $args - the arguments to pass to the retrieval method + * @param object $object - object to retrieve fresh value from + * @param string $method - method name to call + * @param [array] $args - the arguments to pass to the retrieval method * @return mixed - the fresh data */ public function getFresh($object, $method, array $args=[]); diff --git a/src/Aviat/Ion/Cache/CacheManager.php b/src/Ion/Cache/CacheManager.php similarity index 85% rename from src/Aviat/Ion/Cache/CacheManager.php rename to src/Ion/Cache/CacheManager.php index e41ffc3..7f98cdc 100644 --- a/src/Aviat/Ion/Cache/CacheManager.php +++ b/src/Ion/Cache/CacheManager.php @@ -51,9 +51,9 @@ class CacheManager implements CacheInterface { * Retrieve a cached value if it exists, otherwise, get the value * from the passed arguments * - * @param object $object - object to retrieve fresh value from - * @param string $method - method name to call - * @param [array] $args - the arguments to pass to the retrieval method + * @param object $object - object to retrieve fresh value from + * @param string $method - method name to call + * @param [array] $args - the arguments to pass to the retrieval method * @return mixed - the cached or fresh data */ public function get($object, $method, array $args=[]) @@ -73,9 +73,9 @@ class CacheManager implements CacheInterface { /** * Retrieve a fresh value from the method, and update the cache - * @param object $object - object to retrieve fresh value from - * @param string $method - method name to call - * @param [array] $args - the arguments to pass to the retrieval method + * @param object $object - object to retrieve fresh value from + * @param string $method - method name to call + * @param [array] $args - the arguments to pass to the retrieval method * @return mixed - the fresh data */ public function getFresh($object, $method, array $args=[]) @@ -101,7 +101,7 @@ class CacheManager implements CacheInterface { * * @param object $object * @param string $method - * @param array $args + * @param array $args * @return string */ protected function generateHashForMethod($object, $method, array $args) diff --git a/src/Aviat/Ion/Cache/Driver/DriverInterface.php b/src/Ion/Cache/Driver/DriverInterface.php similarity index 96% rename from src/Aviat/Ion/Cache/Driver/DriverInterface.php rename to src/Ion/Cache/Driver/DriverInterface.php index 2f826c2..171a61c 100644 --- a/src/Aviat/Ion/Cache/Driver/DriverInterface.php +++ b/src/Ion/Cache/Driver/DriverInterface.php @@ -28,7 +28,7 @@ interface DriverInterface { * Set a cached value * * @param string $key - * @param mixed $value + * @param mixed $value * @return DriverInterface */ public function set($key, $value); diff --git a/src/Aviat/Ion/Cache/Driver/DriverTrait.php b/src/Ion/Cache/Driver/DriverTrait.php similarity index 100% rename from src/Aviat/Ion/Cache/Driver/DriverTrait.php rename to src/Ion/Cache/Driver/DriverTrait.php diff --git a/src/Aviat/Ion/Cache/Driver/NullDriver.php b/src/Ion/Cache/Driver/NullDriver.php similarity index 97% rename from src/Aviat/Ion/Cache/Driver/NullDriver.php rename to src/Ion/Cache/Driver/NullDriver.php index 863d684..98b9ce6 100644 --- a/src/Aviat/Ion/Cache/Driver/NullDriver.php +++ b/src/Ion/Cache/Driver/NullDriver.php @@ -40,7 +40,7 @@ class NullDriver implements DriverInterface { * Set a cached value * * @param string $key - * @param mixed $value + * @param mixed $value * @return DriverInterface */ public function set($key, $value) diff --git a/src/Aviat/Ion/Cache/Driver/RedisDriver.php b/src/Ion/Cache/Driver/RedisDriver.php similarity index 97% rename from src/Aviat/Ion/Cache/Driver/RedisDriver.php rename to src/Ion/Cache/Driver/RedisDriver.php index dc385aa..073f2e0 100644 --- a/src/Aviat/Ion/Cache/Driver/RedisDriver.php +++ b/src/Ion/Cache/Driver/RedisDriver.php @@ -56,7 +56,7 @@ class RedisDriver implements DriverInterface { */ public function __destruct() { - $this->redis = null; + $this->redis = NULL; } /** @@ -77,7 +77,7 @@ class RedisDriver implements DriverInterface { * Set a cached value * * @param string $rawKey - * @param mixed $value + * @param mixed $value * @return DriverInterface */ public function set($rawKey, $value) diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Ion/Cache/Driver/SQLDriver.php similarity index 98% rename from src/Aviat/Ion/Cache/Driver/SQLDriver.php rename to src/Ion/Cache/Driver/SQLDriver.php index 703128e..28a019c 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Ion/Cache/Driver/SQLDriver.php @@ -75,7 +75,7 @@ class SQLDriver extends DB implements DriverInterface { * Set a cached value * * @param string $key - * @param mixed $value + * @param mixed $value * @return DriverInterface */ public function set($key, $value) diff --git a/src/Aviat/Ion/Config.php b/src/Ion/Config.php similarity index 97% rename from src/Aviat/Ion/Config.php rename to src/Ion/Config.php index c0dea9f..8f72924 100644 --- a/src/Aviat/Ion/Config.php +++ b/src/Ion/Config.php @@ -81,7 +81,7 @@ class Config implements ConfigInterface { * Set a config value * * @param integer|string|array $key - * @param mixed $value + * @param mixed $value * @throws InvalidArgumentException * @return Config */ diff --git a/src/Aviat/Ion/ConfigInterface.php b/src/Ion/ConfigInterface.php similarity index 94% rename from src/Aviat/Ion/ConfigInterface.php rename to src/Ion/ConfigInterface.php index 202d926..65af546 100644 --- a/src/Aviat/Ion/ConfigInterface.php +++ b/src/Ion/ConfigInterface.php @@ -28,7 +28,7 @@ interface ConfigInterface { * Set a config value * * @param integer|string|array $key - * @param mixed $value + * @param mixed $value * @throws \InvalidArgumentException * @return ConfigInterface */ diff --git a/src/Aviat/Ion/Di/Container.php b/src/Ion/Di/Container.php similarity index 97% rename from src/Aviat/Ion/Di/Container.php rename to src/Ion/Di/Container.php index 40ba789..d4b0917 100644 --- a/src/Aviat/Ion/Di/Container.php +++ b/src/Ion/Di/Container.php @@ -86,7 +86,7 @@ class Container implements ContainerInterface { * Add a value to the container * * @param string $id - * @param mixed $value + * @param mixed $value * @return ContainerInterface */ public function set($id, $value) @@ -110,7 +110,7 @@ class Container implements ContainerInterface { /** * Determine whether a logger channel is registered - * @param string $key The logger channel + * @param string $key The logger channel * @return boolean */ public function hasLogger($key = 'default') diff --git a/src/Aviat/Ion/Di/ContainerAware.php b/src/Ion/Di/ContainerAware.php similarity index 100% rename from src/Aviat/Ion/Di/ContainerAware.php rename to src/Ion/Di/ContainerAware.php diff --git a/src/Aviat/Ion/Di/ContainerAwareInterface.php b/src/Ion/Di/ContainerAwareInterface.php similarity index 100% rename from src/Aviat/Ion/Di/ContainerAwareInterface.php rename to src/Ion/Di/ContainerAwareInterface.php diff --git a/src/Aviat/Ion/Di/ContainerInterface.php b/src/Ion/Di/ContainerInterface.php similarity index 93% rename from src/Aviat/Ion/Di/ContainerInterface.php rename to src/Ion/Di/ContainerInterface.php index 703bb0a..f86f71d 100644 --- a/src/Aviat/Ion/Di/ContainerInterface.php +++ b/src/Ion/Di/ContainerInterface.php @@ -23,14 +23,14 @@ interface ContainerInterface extends \Interop\Container\ContainerInterface { * Add a value to the container * * @param string $key - * @param mixed $value + * @param mixed $value * @return ContainerInterface */ public function set($key, $value); /** * Determine whether a logger channel is registered - * @param string $key The logger channel + * @param string $key The logger channel * @return boolean */ public function hasLogger($key = 'default'); diff --git a/src/Aviat/Ion/Di/Exception/ContainerException.php b/src/Ion/Di/Exception/ContainerException.php similarity index 100% rename from src/Aviat/Ion/Di/Exception/ContainerException.php rename to src/Ion/Di/Exception/ContainerException.php diff --git a/src/Aviat/Ion/Di/Exception/NotFoundException.php b/src/Ion/Di/Exception/NotFoundException.php similarity index 100% rename from src/Aviat/Ion/Di/Exception/NotFoundException.php rename to src/Ion/Di/Exception/NotFoundException.php diff --git a/src/Aviat/Ion/Enum.php b/src/Ion/Enum.php similarity index 97% rename from src/Aviat/Ion/Enum.php rename to src/Ion/Enum.php index 5c69179..c2eceae 100644 --- a/src/Aviat/Ion/Enum.php +++ b/src/Ion/Enum.php @@ -37,7 +37,7 @@ abstract class Enum { /** * Verify that a constant value is valid - * @param mixed $key + * @param mixed $key * @return boolean */ protected function isValid($key) diff --git a/src/Aviat/Ion/Exception/ConfigException.php b/src/Ion/Exception/ConfigException.php similarity index 100% rename from src/Aviat/Ion/Exception/ConfigException.php rename to src/Ion/Exception/ConfigException.php diff --git a/src/Ion/Exception/DoubleRenderException.php b/src/Ion/Exception/DoubleRenderException.php new file mode 100644 index 0000000..556c0cc --- /dev/null +++ b/src/Ion/Exception/DoubleRenderException.php @@ -0,0 +1,31 @@ +config = $config; - $this->db_config = (array)$config->get('database'); - } -} -// End of DB.php \ No newline at end of file +config = $config; + $this->db_config = (array)$config->get('database'); + } +} +// End of DB.php diff --git a/src/Aviat/Ion/StaticInstance.php b/src/Ion/StaticInstance.php similarity index 96% rename from src/Aviat/Ion/StaticInstance.php rename to src/Ion/StaticInstance.php index 7a67b93..3750ea6 100644 --- a/src/Aviat/Ion/StaticInstance.php +++ b/src/Ion/StaticInstance.php @@ -30,7 +30,7 @@ trait StaticInstance { * * @codeCoverageIgnore * @param string $method - * @param array $args + * @param array $args * @retun mixed */ public function __call($method, $args) @@ -46,7 +46,7 @@ trait StaticInstance { * an instance of the class isn't required * * @param string $method - * @param array $args + * @param array $args * @return mixed */ public static function __callStatic($method, $args) diff --git a/src/Aviat/Ion/StringWrapper.php b/src/Ion/StringWrapper.php similarity index 100% rename from src/Aviat/Ion/StringWrapper.php rename to src/Ion/StringWrapper.php diff --git a/src/Aviat/Ion/Transformer/AbstractTransformer.php b/src/Ion/Transformer/AbstractTransformer.php similarity index 100% rename from src/Aviat/Ion/Transformer/AbstractTransformer.php rename to src/Ion/Transformer/AbstractTransformer.php diff --git a/src/Aviat/Ion/Transformer/TransformerInterface.php b/src/Ion/Transformer/TransformerInterface.php similarity index 100% rename from src/Aviat/Ion/Transformer/TransformerInterface.php rename to src/Ion/Transformer/TransformerInterface.php diff --git a/src/Aviat/Ion/Type/ArrayType.php b/src/Ion/Type/ArrayType.php similarity index 97% rename from src/Aviat/Ion/Type/ArrayType.php rename to src/Ion/Type/ArrayType.php index c3d78f8..7df4ff3 100644 --- a/src/Aviat/Ion/Type/ArrayType.php +++ b/src/Ion/Type/ArrayType.php @@ -80,7 +80,7 @@ class ArrayType { * Call one of the dynamically created methods * * @param string $method - * @param array $args + * @param array $args * @return mixed * @throws \InvalidArgumentException */ @@ -120,8 +120,8 @@ class ArrayType { /** * Fill an array with the specified value * - * @param int $start_index - * @param int $num + * @param int $start_index + * @param int $num * @param mixed $value * @return array */ @@ -145,7 +145,7 @@ class ArrayType { * Find an array key by its associated value * * @param mixed $value - * @param bool $strict + * @param bool $strict * @return false|integer|string */ public function search($value, $strict = FALSE) @@ -157,7 +157,7 @@ class ArrayType { * Determine if the array has the passed value * * @param mixed $value - * @param bool $strict + * @param bool $strict * @return bool */ public function has($value, $strict = FALSE) @@ -233,7 +233,7 @@ class ArrayType { * Sets the value of an arbitrarily deep key in the array * and returns the modified array * - * @param array $key + * @param array $key * @param mixed $value * @return array */ diff --git a/src/Aviat/Ion/Type/StringType.php b/src/Ion/Type/StringType.php similarity index 100% rename from src/Aviat/Ion/Type/StringType.php rename to src/Ion/Type/StringType.php diff --git a/src/Aviat/Ion/View.php b/src/Ion/View.php similarity index 64% rename from src/Aviat/Ion/View.php rename to src/Ion/View.php index daaece4..f26ad41 100644 --- a/src/Aviat/Ion/View.php +++ b/src/Ion/View.php @@ -12,13 +12,16 @@ namespace Aviat\Ion; +use Psr\Http\Message\ResponseInterface; + use Aviat\Ion\Di\ContainerInterface; +use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\Type\StringType; /** * Base view response class */ -abstract class View { +abstract class View /* partially */ implements ViewInterface { use Di\ContainerAware; use StringWrapper; @@ -26,31 +29,10 @@ abstract class View { /** * HTTP response Object * - * @var Zend\Diactoros\Response + * @var ResponseInterface */ public $response; - /** - * Redirect response object - * - * @var Zend\Diactoros\RedirectResponse - */ - protected $redirectResponse; - - /** - * Response mime type - * - * @var string - */ - protected $contentType = ''; - - /** - * String of response to be output - * - * @var StringType - */ - protected $output; - /** * If the view has sent output via * __toString or send method @@ -68,7 +50,6 @@ abstract class View { { $this->setContainer($container); $this->response = $container->get('response'); - $this->redirectResponse = NULL; } /** @@ -83,21 +64,20 @@ abstract class View { } /** - * Return rendered output - * - * @return string + * @inheritdoc */ public function __toString() { + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } $this->hasRendered = TRUE; return $this->getOutput(); } /** - * Set the output string - * - * @param string $string - * @return View + * @inheritdoc */ public function setOutput($string) { @@ -107,10 +87,7 @@ abstract class View { } /** - * Append additional output - * - * @param string $string - * @return View + * @inheritdoc */ public function appendOutput($string) { @@ -118,20 +95,11 @@ abstract class View { } /** - * Get the current output string - * - * @return string + * @inheritdoc */ public function getOutput() { return $this->response->getBody()->__toString(); } - - /** - * Send output to client - * - * @return void - */ - abstract public function send(); } // End of View.php \ No newline at end of file diff --git a/src/Aviat/Ion/View/HtmlView.php b/src/Ion/View/HtmlView.php similarity index 97% rename from src/Aviat/Ion/View/HtmlView.php rename to src/Ion/View/HtmlView.php index d73cad0..01af7a8 100644 --- a/src/Aviat/Ion/View/HtmlView.php +++ b/src/Ion/View/HtmlView.php @@ -26,6 +26,13 @@ class HtmlView extends HttpView { */ protected $helper; + /** + * Response mime type + * + * @var string + */ + protected $contentType = 'text/html'; + /** * Create the Html View * @@ -37,18 +44,11 @@ class HtmlView extends HttpView { $this->helper = $container->get('html-helper'); } - /** - * Response mime type - * - * @var string - */ - protected $contentType = 'text/html'; - /** * Render a basic html Template * * @param string $path - * @param array $data + * @param array $data * @return string */ public function render_template($path, $data) diff --git a/src/Aviat/Ion/View/HttpView.php b/src/Ion/View/HttpView.php similarity index 80% rename from src/Aviat/Ion/View/HttpView.php rename to src/Ion/View/HttpView.php index 7ff97f0..71c134b 100644 --- a/src/Aviat/Ion/View/HttpView.php +++ b/src/Ion/View/HttpView.php @@ -12,8 +12,9 @@ namespace Aviat\Ion\View; -use Zend\Diactoros\Response; use Zend\Diactoros\Response\SapiEmitter; + +use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\View as BaseView; /** @@ -21,12 +22,19 @@ use Aviat\Ion\View as BaseView; */ class HttpView extends BaseView { + /** + * Response mime type + * + * @var string + */ + protected $contentType = ''; + /** * Do a redirect * * @codeCoverageIgnore * @param string $url - * @param int $code + * @param int $code * @return void */ public function redirect($url, $code) @@ -60,13 +68,10 @@ class HttpView extends BaseView { } /** - * Send output to client - * - * @return void + * @inheritdoc */ public function send() { - $this->hasRendered = TRUE; $this->output(); } @@ -74,11 +79,17 @@ class HttpView extends BaseView { * Send the appropriate response * * @codeCoverageIgnore + * @throws DoubleRenderException * @return void */ protected function output() { - $this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8") + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->response = $this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8") ->withHeader('Content-Security-Policy', "script-src 'self'") ->withHeader('X-Content-Type-Options', 'nosniff') ->withHeader('X-XSS-Protection', '1;mode=block') @@ -86,6 +97,7 @@ class HttpView extends BaseView { $sender = new SapiEmitter($this->response); $sender->emit($this->response); - } + $this->hasRendered = TRUE; + } } \ No newline at end of file diff --git a/src/Aviat/Ion/View/JsonView.php b/src/Ion/View/JsonView.php similarity index 88% rename from src/Aviat/Ion/View/JsonView.php rename to src/Ion/View/JsonView.php index a65dd55..6474609 100644 --- a/src/Aviat/Ion/View/JsonView.php +++ b/src/Ion/View/JsonView.php @@ -13,8 +13,7 @@ namespace Aviat\Ion\View; use Aviat\Ion\Json; -use Aviat\Ion\View\HttpView; -use Aviat\Ion\View as BaseView; +use Aviat\Ion\ViewInterface; /** * View class to serialize Json @@ -32,7 +31,7 @@ class JsonView extends HttpView { * Set the output string * * @param mixed $string - * @return BaseView + * @return ViewInterface */ public function setOutput($string) { diff --git a/src/Ion/ViewInterface.php b/src/Ion/ViewInterface.php new file mode 100644 index 0000000..91a2514 --- /dev/null +++ b/src/Ion/ViewInterface.php @@ -0,0 +1,62 @@ +expectException(DoubleRenderException::class); + $this->expectExceptionMessage('A view can only be rendered once, because headers can only be sent once.'); + + throw new DoubleRenderException(); + } +} \ No newline at end of file diff --git a/tests/Ion/JsonTest.php b/tests/Ion/JsonTest.php index f9ec164..212f8f8 100644 --- a/tests/Ion/JsonTest.php +++ b/tests/Ion/JsonTest.php @@ -1,9 +1,11 @@ view = new TestHtmlView($this->container); + $this->view = new \TestHtmlView($this->container); } public function testRenderTemplate() diff --git a/tests/Ion/View/HttpViewTest.php b/tests/Ion/View/HttpViewTest.php index 9f57a14..624a727 100644 --- a/tests/Ion/View/HttpViewTest.php +++ b/tests/Ion/View/HttpViewTest.php @@ -1,8 +1,11 @@ view = new TestHttpView($this->container); + $this->view = new \TestHttpView($this->container); $this->friend = new Friend($this->view); } @@ -43,4 +46,28 @@ class HttpViewTest extends Ion_TestCase { $view = $this->view->setStatusCode(404); $this->assertEquals(404, $view->response->getStatusCode()); } + + public function testSendDoubleRenderException() + { + $this->expectException(DoubleRenderException::class); + $this->expectExceptionMessage('A view can only be rendered once, because headers can only be sent once.'); + + // First render + $this->view->__toString(); + + // Second render + $this->view->send(); + } + + public function test__toStringDoubleRenderException() + { + $this->expectException(DoubleRenderException::class); + $this->expectExceptionMessage('A view can only be rendered once, because headers can only be sent once.'); + + // First render + $this->view->send(); + + // Second render + $this->view->__toString(); + } } \ No newline at end of file diff --git a/tests/Ion/View/JsonViewTest.php b/tests/Ion/View/JsonViewTest.php index 0c21bb5..19cc636 100644 --- a/tests/Ion/View/JsonViewTest.php +++ b/tests/Ion/View/JsonViewTest.php @@ -1,8 +1,8 @@ view = new TestJsonView($this->container); + $this->view = new \TestJsonView($this->container); $this->friend = new Friend($this->view); } public function testSetOutputJSON() { // Extend view class to remove destructor which does output - $view = new TestJsonView($this->container); + $view = new \TestJsonView($this->container); // Json encode non-string $content = ['foo' => 'bar']; @@ -29,7 +29,7 @@ class JsonViewTest extends HttpViewTest { public function testSetOutput() { // Directly set string - $view = new TestJsonView($this->container); + $view = new \TestJsonView($this->container); $content = '{}'; $expected = '{}'; $view->setOutput($content); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2002d77..a1f1e4b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -35,30 +35,13 @@ require 'Ion_TestCase.php'; // Composer autoload require __DIR__ . '/../vendor/autoload.php'; -/** - * Set up autoloaders - * - * @codeCoverageIgnore - * @return void - */ -spl_autoload_register(function ($class) { - $class_parts = explode('\\', $class); - $ns_path = realpath(__DIR__ . '/../src') . '/' . implode('/', $class_parts) . ".php"; - - if (file_exists($ns_path)) - { - require_once($ns_path); - return; - } -}); - // ----------------------------------------------------------------------------- // Ini Settings // ----------------------------------------------------------------------------- ini_set('session.use_cookies', 0); ini_set("session.use_only_cookies",0); ini_set("session.use_trans_sid",1); -// Start session here to supress error about headers not sent +// Start session here to surpress error about headers not sent session_start(); // ----------------------------------------------------------------------------- diff --git a/tests/mocks.php b/tests/mocks.php index c93dfcd..b83fcf5 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -4,6 +4,7 @@ */ use Aviat\Ion\Enum; +use Aviat\Ion\Exception\DoubleRenderException; use Aviat\Ion\Friend; use Aviat\Ion\Transformer\AbstractTransformer; use Aviat\Ion\View; @@ -70,7 +71,7 @@ class TestTransformer extends AbstractTransformer { } trait MockViewOutputTrait { - protected function output() { + /*protected function output() { $reflect = new ReflectionClass($this); $properties = $reflect->getProperties(); $props = []; @@ -88,31 +89,69 @@ trait MockViewOutputTrait { $friend->__set($name, $val); } - $friend->output(); + //$friend->output(); + }*/ + + public function send() + { + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->hasRendered = TRUE; } } class TestView extends View { - public function send() {} - protected function output() + public function send() { - /*$content =& $this->response->content; - $content->set($this->output); - $content->setType($this->contentType); - $content->setCharset('utf-8');*/ + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->hasRendered = TRUE; } + public function output() {} } class TestHtmlView extends HtmlView { - use MockViewOutputTrait; + protected function output() + { + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->hasRendered = TRUE; + } } class TestHttpView extends HttpView { - use MockViewOutputTrait; + protected function output() + { + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->hasRendered = TRUE; + } } class TestJsonView extends JsonView { public function __destruct() {} + + protected function output() + { + if ($this->hasRendered) + { + throw new DoubleRenderException(); + } + + $this->hasRendered = TRUE; + } } // -----------------------------------------------------------------------------