From 90a47947d9a50f854c6614e14ca416676a32cff0 Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 31 Jan 2022 23:34:21 +0000 Subject: [PATCH] Remove Stream Wrapper Functionality - Updates the path stack template resolver removing deprecated methods related to stream wrapper - Removes the Stream wrapper class - Updates psalm baseline Signed-off-by: George Steel --- psalm-baseline.xml | 32 +--- src/Resolver/TemplatePathStack.php | 61 +------ src/Stream.php | 204 ------------------------ test/Resolver/TemplatePathStackTest.php | 46 +----- 4 files changed, 5 insertions(+), 338 deletions(-) delete mode 100644 src/Stream.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 724fe8da4..8b2de156b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2049,7 +2049,7 @@ false false - + $value $value $value @@ -2063,7 +2063,7 @@ $this->paths - + (bool) $flag (bool) $flag (string) $defaultSuffix @@ -2120,34 +2120,6 @@ plugin - - - $data - $stat - - - $mode - $offset - $opened_path - $options - $whence - - - $this->pos - $this->pos - $this->pos - - - $offset - $offset - $offset - - - break; - break; - break; - - 'ArrayIterator' diff --git a/src/Resolver/TemplatePathStack.php b/src/Resolver/TemplatePathStack.php index 8781afa1a..16fa9c2ac 100644 --- a/src/Resolver/TemplatePathStack.php +++ b/src/Resolver/TemplatePathStack.php @@ -7,7 +7,6 @@ use Laminas\Stdlib\SplStack; use Laminas\View\Exception; use Laminas\View\Renderer\RendererInterface as Renderer; -use Laminas\View\Stream; use SplFileInfo; use Traversable; @@ -15,8 +14,6 @@ use function file_exists; use function get_class; use function gettype; -use function in_array; -use function ini_get; use function is_array; use function is_object; use function is_string; @@ -25,8 +22,6 @@ use function preg_match; use function rtrim; use function sprintf; -use function stream_get_wrappers; -use function stream_wrapper_register; use function strpos; use function strtolower; @@ -69,15 +64,6 @@ class TemplatePathStack implements ResolverInterface */ protected $lfiProtectionOn = true; - /**@+ - * Flags used to determine if a stream wrapper should be used for enabling short tags - */ - - /** @var bool */ - protected $useViewStream = false; - /** @var bool */ - protected $useStreamWrapper = false; - /**@-*/ /** @@ -87,14 +73,6 @@ class TemplatePathStack implements ResolverInterface */ public function __construct($options = null) { - $this->useViewStream = (bool) ini_get('short_open_tag'); - if ($this->useViewStream) { - if (! in_array('laminas.view', stream_get_wrappers())) { - /** @psalm-suppress DeprecatedClass */ - stream_wrapper_register('laminas.view', Stream::class); - } - } - /** @psalm-var PathStack $paths */ $paths = new SplStack(); $this->paths = $paths; @@ -128,10 +106,6 @@ public function setOptions($options) case 'script_paths': $this->addPaths($value); break; - case 'use_stream_wrapper': - /** @psalm-suppress DeprecatedMethod */ - $this->setUseStreamWrapper($value); - break; case 'default_suffix': $this->setDefaultSuffix($value); break; @@ -283,35 +257,6 @@ public function isLfiProtectionOn() return $this->lfiProtectionOn; } - /** - * Set flag indicating if stream wrapper should be used if short_open_tag is off - * - * @deprecated will be removed in version 3 - * - * @param bool $flag - * @return TemplatePathStack - */ - public function setUseStreamWrapper($flag) - { - $this->useStreamWrapper = (bool) $flag; - return $this; - } - - /** - * Should the stream wrapper be used if short_open_tag is off? - * - * Returns true if the use_stream_wrapper flag is set, and if short_open_tag - * is disabled. - * - * @deprecated will be removed in version 3 - * - * @return bool - */ - public function useStreamWrapper() - { - return $this->useViewStream && $this->useStreamWrapper; - } - /** * Retrieve the filesystem path to a view script * @@ -351,11 +296,7 @@ public function resolve($name, ?Renderer $renderer = null) break; } } - /** @psalm-suppress DeprecatedMethod */ - if ($this->useStreamWrapper()) { - // If using a stream wrapper, prepend the spec to the path - $filePath = 'laminas.view://' . $filePath; - } + return $filePath; } } diff --git a/src/Stream.php b/src/Stream.php deleted file mode 100644 index b0829c232..000000000 --- a/src/Stream.php +++ /dev/null @@ -1,204 +0,0 @@ - and ) should be used - */ -class Stream -{ - /** - * Current stream position. - * - * @var int - */ - protected $pos = 0; - - /** - * Data for streaming. - * - * @var string - */ - protected $data; - - /** - * Stream stats. - * - * @var array - */ - protected $stat; - - /** - * Opens the script file and converts markup. - * - * @param string $path - * @param $mode - * @param $options - * @param $opened_path - * @return bool - */ - // @codingStandardsIgnoreStart - public function stream_open($path, $mode, $options, &$opened_path) - { - // @codingStandardsIgnoreEnd - // get the view script source - $path = str_replace('laminas.view://', '', $path); - $this->data = file_get_contents($path); - - /** - * If reading the file failed, update our local stat store - * to reflect the real stat of the file, then return on failure - */ - if ($this->data === false) { - $this->stat = stat($path); - return false; - } - - /** - * Convert to long-form and to - */ - $this->data = preg_replace('/\<\?\=/', "data); - $this->data = preg_replace('/<\?(?!xml|php)/s', 'data); - - /** - * file_get_contents() won't update PHP's stat cache, so we grab a stat - * of the file to prevent additional reads should the script be - * requested again, which will make include() happy. - */ - $this->stat = stat($path); - - return true; - } - - /** - * Included so that __FILE__ returns the appropriate info - * - * @return array - */ - // @codingStandardsIgnoreStart - public function url_stat() - { - // @codingStandardsIgnoreEnd - return $this->stat; - } - - /** - * Reads from the stream. - * - * @param int $count - * @return string - */ - // @codingStandardsIgnoreStart - public function stream_read($count) - { - // @codingStandardsIgnoreEnd - $ret = substr($this->data, $this->pos, $count); - $this->pos += strlen($ret); - return $ret; - } - - /** - * Tells the current position in the stream. - * - * @return int - */ - // @codingStandardsIgnoreStart - public function stream_tell() - { - // @codingStandardsIgnoreEnd - return $this->pos; - } - - /** - * Tells if we are at the end of the stream. - * - * @return bool - */ - // @codingStandardsIgnoreStart - public function stream_eof() - { - // @codingStandardsIgnoreEnd - return $this->pos >= strlen($this->data); - } - - /** - * Stream statistics. - * - * @return array - */ - // @codingStandardsIgnoreStart - public function stream_stat() - { - // @codingStandardsIgnoreEnd - return $this->stat; - } - - /** - * Seek to a specific point in the stream. - * - * @param $offset - * @param $whence - * @return bool - */ - // @codingStandardsIgnoreStart - public function stream_seek($offset, $whence) - { - // @codingStandardsIgnoreEnd - switch ($whence) { - case SEEK_SET: - if ($offset < strlen($this->data) && $offset >= 0) { - $this->pos = $offset; - return true; - } else { - return false; - } - break; - - case SEEK_CUR: - if ($offset >= 0) { - $this->pos += $offset; - return true; - } else { - return false; - } - break; - - case SEEK_END: - if (strlen($this->data) + $offset >= 0) { - $this->pos = strlen($this->data) + $offset; - return true; - } else { - return false; - } - break; - - default: - return false; - } - } -} diff --git a/test/Resolver/TemplatePathStackTest.php b/test/Resolver/TemplatePathStackTest.php index d67ca1a1a..76fdd2a3c 100644 --- a/test/Resolver/TemplatePathStackTest.php +++ b/test/Resolver/TemplatePathStackTest.php @@ -12,7 +12,6 @@ use function array_reverse; use function array_unshift; -use function ini_get; use function realpath; use const DIRECTORY_SEPARATOR; @@ -107,24 +106,6 @@ public function testMayDisableLfiProtection(): void $this->assertFalse($this->stack->isLfiProtectionOn()); } - public function testStreamWrapperDisabledByDefault(): void - { - /** @psalm-suppress DeprecatedMethod */ - $this->assertFalse($this->stack->useStreamWrapper()); - } - - public function testMayEnableStreamWrapper(): void - { - $flag = (bool) ini_get('short_open_tag'); - if (! $flag) { - $this->markTestSkipped('Short tags are disabled; cannot test'); - } - /** @psalm-suppress DeprecatedMethod */ - $this->stack->setUseStreamWrapper(true); - /** @psalm-suppress DeprecatedMethod */ - $this->assertTrue($this->stack->useStreamWrapper()); - } - public function testDoesNotAllowParentDirectoryTraversalByDefault(): void { $this->stack->addPath($this->baseDir . '/_templates'); @@ -164,20 +145,6 @@ public function testReturnsFullPathNameWhenAbleToResolveScriptPath(): void $this->assertEquals($expected, $test); } - public function testReturnsPathWithStreamProtocolWhenStreamWrapperEnabled(): void - { - $flag = (bool) ini_get('short_open_tag'); - if (! $flag) { - $this->markTestSkipped('Short tags are disabled; cannot test'); - } - /** @psalm-suppress DeprecatedMethod */ - $this->stack->setUseStreamWrapper(true) - ->addPath($this->baseDir . '/_templates'); - $expected = 'laminas.view://' . realpath($this->baseDir . '/_templates/test.phtml'); - $test = $this->stack->resolve('test.phtml'); - $this->assertEquals($expected, $test); - } - /** * @psalm-return array */ @@ -209,9 +176,8 @@ public function testSettingOptionsWithInvalidArgumentRaisesException($options): public function validOptions(): array { $options = [ - 'lfi_protection' => false, - 'use_stream_wrapper' => true, - 'default_suffix' => 'php', + 'lfi_protection' => false, + 'default_suffix' => 'php', ]; return [ [$options], @@ -229,10 +195,6 @@ public function testAllowsSettingOptions($options): void $this->stack->setOptions($options); $this->assertFalse($this->stack->isLfiProtectionOn()); - $expected = (bool) ini_get('short_open_tag'); - /** @psalm-suppress DeprecatedMethod */ - $this->assertSame($expected, $this->stack->useStreamWrapper()); - $this->assertSame($options['default_suffix'], $this->stack->getDefaultSuffix()); $this->assertEquals(array_reverse($this->paths), $this->stack->getPaths()->toArray()); @@ -248,10 +210,6 @@ public function testAllowsPassingOptionsToConstructor($options): void $stack = new TemplatePathStack($options); $this->assertFalse($stack->isLfiProtectionOn()); - $expected = (bool) ini_get('short_open_tag'); - /** @psalm-suppress DeprecatedMethod */ - $this->assertSame($expected, $stack->useStreamWrapper()); - $this->assertEquals(array_reverse($this->paths), $stack->getPaths()->toArray()); }