From 0cdcfa147cd2d36cdc9140cb99f37b9739039787 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Mon, 7 Aug 2017 12:25:45 +0300 Subject: [PATCH 01/12] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f383c03ba..f3ecb981d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG for 0.9.0 RC ====================== +1.0.4 +----- +- ability to add conditions to skip vlaidation rules + 1.0.2 (04.05.2017) ----- - ValidatesEntity now caches last set of produced errors From 2a6868da820a2d2a54c8505e79233cf19a565536 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Mon, 7 Aug 2017 12:25:54 +0300 Subject: [PATCH 02/12] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3ecb981d..5b0288ac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ CHANGELOG for 0.9.0 RC 1.0.4 ----- -- ability to add conditions to skip vlaidation rules +- ability to add conditions to skip validation rules 1.0.2 (04.05.2017) ----- From cf5e186ea4f8bdd9a7e4d9cd4e5b9978fe81ca55 Mon Sep 17 00:00:00 2001 From: Lev Seleznev Date: Mon, 7 Aug 2017 15:03:46 +0300 Subject: [PATCH 03/12] View cache file locator --- source/Spiral/Views/AbstractViewCache.php | 32 ++++++++++++ .../Views/Engines/Stempler/StemplerCache.php | 6 ++- .../Spiral/Views/Engines/Twig/TwigCache.php | 6 ++- source/Spiral/Views/ViewCacheLocator.php | 52 +++++++++++++++++++ tests/Views/StemplerTest.php | 33 ++++++++++++ 5 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 source/Spiral/Views/AbstractViewCache.php create mode 100644 source/Spiral/Views/ViewCacheLocator.php diff --git a/source/Spiral/Views/AbstractViewCache.php b/source/Spiral/Views/AbstractViewCache.php new file mode 100644 index 000000000..324347fe2 --- /dev/null +++ b/source/Spiral/Views/AbstractViewCache.php @@ -0,0 +1,32 @@ +getFilename() . '.' . $this->environment->getID()); - return $this->environment->cacheDirectory() . $hash . '.php'; + return $this->environment->cacheDirectory() . + $this->getPrefix($context->getName(), $context->getNamespace()) . '-' . $hash . '.php'; } /** diff --git a/source/Spiral/Views/Engines/Twig/TwigCache.php b/source/Spiral/Views/Engines/Twig/TwigCache.php index 87139c7d7..520acaca6 100644 --- a/source/Spiral/Views/Engines/Twig/TwigCache.php +++ b/source/Spiral/Views/Engines/Twig/TwigCache.php @@ -9,12 +9,13 @@ namespace Spiral\Views\Engines\Twig; use Spiral\Files\FilesInterface; +use Spiral\Views\AbstractViewCache; use Spiral\Views\EnvironmentInterface; /** * Spiral specific twig cache. OpCache reset not included yet. */ -class TwigCache implements \Twig_CacheInterface +class TwigCache extends AbstractViewCache implements \Twig_CacheInterface { /** * @var FilesInterface @@ -41,9 +42,10 @@ public function __construct(FilesInterface $files, EnvironmentInterface $environ */ public function generateKey($name, $className) { + $prefix = $this->getPrefix($name); $hash = hash('md5', $className . '.' . $this->environment->getID()); - return "{$this->environment->cacheDirectory()}/{$hash}.php"; + return "{$this->environment->cacheDirectory()}/{$prefix}-{$hash}.php"; } /** diff --git a/source/Spiral/Views/ViewCacheLocator.php b/source/Spiral/Views/ViewCacheLocator.php new file mode 100644 index 000000000..5d2785cef --- /dev/null +++ b/source/Spiral/Views/ViewCacheLocator.php @@ -0,0 +1,52 @@ +config = $config; + $this->fileManager = $fileManager; + } + + /** + * Returns all cache files for view. + * + * @param string $view + * @param string $namespace + * @return array + */ + public function getFiles($view, $namespace = 'default') + { + $prefix = $this->getPrefix($view, $namespace); + + return $this->fileManager->getFiles($this->config->cacheDirectory(), $prefix.'-*'); + } +} \ No newline at end of file diff --git a/tests/Views/StemplerTest.php b/tests/Views/StemplerTest.php index 50c0692a7..4649c03e2 100644 --- a/tests/Views/StemplerTest.php +++ b/tests/Views/StemplerTest.php @@ -10,10 +10,20 @@ use Spiral\Tests\BaseTest; use Spiral\Views\Engines\Stempler\StemplerCache; use Spiral\Views\Engines\Stempler\StemplerView; +use Spiral\Views\ViewCacheLocator; use Spiral\Views\ViewLoader; class StemplerTest extends BaseTest { + protected function deleteCacheFiles() + { + foreach ($this->files->getFiles($this->views->getEnvironment()->cacheDirectory()) as $filename) { + //If exception is thrown here this will mean that application wasn't correctly + //destructed and there is open resources kept + $this->files->delete($filename); + } + } + public function testRenderSimple() { $this->assertSame('Hello, World!', trim($this->views->render('home', [ @@ -92,4 +102,27 @@ public function testView() { $this->assertInstanceOf(StemplerView::class, $this->views->get('home')); } + + public function testCacheLocator() + { + $this->deleteCacheFiles(); + clearstatcache(); + + $this->views->withEnvironment( + $this->views->getEnvironment()->withDependency('value', function () { + return 'test-one'; + }) + )->compile('home'); + + $this->views->withEnvironment( + $this->views->getEnvironment()->withDependency('value', function () { + return 'test-two'; + }) + )->compile('home'); + + /** @var ViewCacheLocator $data */ + $viewCacheLocator = $this->container->get(ViewCacheLocator::class); + + $this->assertSame(2, count($viewCacheLocator->getFiles('home'))); + } } \ No newline at end of file From 824d5cb35fd3b8764f8a43b43679be5ab22f22a9 Mon Sep 17 00:00:00 2001 From: Lev Seleznev Date: Tue, 8 Aug 2017 12:56:36 +0300 Subject: [PATCH 04/12] fix tests --- tests/Translator/LocatorTest.php | 7 ++++++- tests/Views/StemplerTest.php | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/Translator/LocatorTest.php b/tests/Translator/LocatorTest.php index 8f2931170..96b4fb30b 100644 --- a/tests/Translator/LocatorTest.php +++ b/tests/Translator/LocatorTest.php @@ -49,7 +49,12 @@ public function testGetLocales() $config->shouldReceive('localesDirectory')->andReturn(__DIR__ . '/fixtures/locales/'); - $this->assertSame(['en', 'ru'], $source->getLocales()); + $compared = $source->getLocales(); + $shouldBe = ['en', 'ru']; + sort($shouldBe); + sort($compared); + + $this->assertSame($shouldBe, $compared); } public function testLoadLocale() diff --git a/tests/Views/StemplerTest.php b/tests/Views/StemplerTest.php index 4649c03e2..9fac91b77 100644 --- a/tests/Views/StemplerTest.php +++ b/tests/Views/StemplerTest.php @@ -63,7 +63,10 @@ public function testCompileWithEnvironment() public function testRenderFromOtherLoader() { - $this->assertSame('Hello, World!', $this->views->render('native', [ + $this->deleteCacheFiles(); + clearstatcache(); + + $this->assertContains('Hello, World!', $this->views->render('home', [ 'name' => 'World' ])); @@ -74,7 +77,7 @@ public function testRenderFromOtherLoader() ) ); - $this->assertSame('home alt', $views->render('home')); + $this->assertContains('home alt', $views->render('home')); } public function testRenderFromCache() From 4e2adbaaa24714dbf97960a22994dd246c1c986b Mon Sep 17 00:00:00 2001 From: Lev Seleznev Date: Tue, 8 Aug 2017 13:15:42 +0300 Subject: [PATCH 05/12] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b0288ac8..6c4ecfbc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ CHANGELOG for 0.9.0 RC ====================== +1.0.5 +----- +- ability to locate view cache files by view name and namespace 1.0.4 ----- From 26c912e4dc35d4ed9873eec29075e9aeda886258 Mon Sep 17 00:00:00 2001 From: Valentin V Date: Thu, 28 Sep 2017 10:38:17 +0300 Subject: [PATCH 06/12] Fix typo in `originateErrors` method --- source/Spiral/Http/Request/InputMapper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Spiral/Http/Request/InputMapper.php b/source/Spiral/Http/Request/InputMapper.php index 28eccf336..89f1eb167 100644 --- a/source/Spiral/Http/Request/InputMapper.php +++ b/source/Spiral/Http/Request/InputMapper.php @@ -113,7 +113,7 @@ public function originateErrors(array $errors): array $this->mountMessage($mapped, $this->schema[$field]['origin'], $message); } else { //Custom error - $mapped[$field] = $mapped; + $mapped[$field] = $message; } } @@ -264,4 +264,4 @@ private function createOrigins( return $result; } -} \ No newline at end of file +} From d9f85267ef62bdcf2921f7d524d45cdce909199f Mon Sep 17 00:00:00 2001 From: valentin v / vvval Date: Thu, 28 Sep 2017 13:02:34 +0300 Subject: [PATCH 07/12] PR Fix validation for more than one sub request (3 depth levels for example) --- source/Spiral/Http/Request/InputInterface.php | 4 +- source/Spiral/Http/Request/InputManager.php | 10 +++- tests/Http/Fixtures/FirstDepthRequest.php | 12 +++++ tests/Http/Fixtures/SecondDepthRequest.php | 12 +++++ tests/Http/Fixtures/ThirdDepthRequest.php | 12 +++++ .../MultipleDepthRequestTest.php | 47 +++++++++++++++++++ tests/Http/RequestFilters/ScopingTest.php | 2 +- 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 tests/Http/Fixtures/FirstDepthRequest.php create mode 100644 tests/Http/Fixtures/SecondDepthRequest.php create mode 100644 tests/Http/Fixtures/ThirdDepthRequest.php create mode 100644 tests/Http/RequestFilters/MultipleDepthRequestTest.php diff --git a/source/Spiral/Http/Request/InputInterface.php b/source/Spiral/Http/Request/InputInterface.php index 4c77bcbc0..df7b8f7d9 100644 --- a/source/Spiral/Http/Request/InputInterface.php +++ b/source/Spiral/Http/Request/InputInterface.php @@ -36,7 +36,9 @@ public function getValue(string $source, string $name = null); * * @param string $prefix * + * @param bool $add When set to false current prefix path will be overwritten. + * * @return InputInterface */ - public function withPrefix(string $prefix): InputInterface; + public function withPrefix(string $prefix, bool $add = true): InputInterface; } \ No newline at end of file diff --git a/source/Spiral/Http/Request/InputManager.php b/source/Spiral/Http/Request/InputManager.php index 621fa95bf..d6a95fb4b 100644 --- a/source/Spiral/Http/Request/InputManager.php +++ b/source/Spiral/Http/Request/InputManager.php @@ -387,10 +387,16 @@ public function getValue(string $source, string $name = null) * * @return self */ - public function withPrefix(string $prefix): InputInterface + public function withPrefix(string $prefix, bool $add = true): InputInterface { $input = clone $this; - $input->prefix = $prefix; + + if ($add) { + $input->prefix .= '.' . $prefix; + $input->prefix = trim($input->prefix, '.'); + } else { + $input->prefix = $prefix; + } return $input; } diff --git a/tests/Http/Fixtures/FirstDepthRequest.php b/tests/Http/Fixtures/FirstDepthRequest.php new file mode 100644 index 000000000..7ae04357a --- /dev/null +++ b/tests/Http/Fixtures/FirstDepthRequest.php @@ -0,0 +1,12 @@ + SecondDepthRequest::class + ]; +} \ No newline at end of file diff --git a/tests/Http/Fixtures/SecondDepthRequest.php b/tests/Http/Fixtures/SecondDepthRequest.php new file mode 100644 index 000000000..d3a1f2223 --- /dev/null +++ b/tests/Http/Fixtures/SecondDepthRequest.php @@ -0,0 +1,12 @@ + ThirdDepthRequest::class + ]; +} \ No newline at end of file diff --git a/tests/Http/Fixtures/ThirdDepthRequest.php b/tests/Http/Fixtures/ThirdDepthRequest.php new file mode 100644 index 000000000..001c9a129 --- /dev/null +++ b/tests/Http/Fixtures/ThirdDepthRequest.php @@ -0,0 +1,12 @@ + 'data:third' + ]; +} \ No newline at end of file diff --git a/tests/Http/RequestFilters/MultipleDepthRequestTest.php b/tests/Http/RequestFilters/MultipleDepthRequestTest.php new file mode 100644 index 000000000..a74807e49 --- /dev/null +++ b/tests/Http/RequestFilters/MultipleDepthRequestTest.php @@ -0,0 +1,47 @@ +withParsedBody([]); + $this->container->bind(ServerRequestInterface::class, $serverRequest); + + /** @var DemoRequest $request */ + $request = $this->container->get(FirstDepthRequest::class); + + $this->assertFalse($request->isValid()); + $this->assertArrayHasKey('first', $request->getErrors()); + $this->assertArrayHasKey('second', $request->getErrors()['first']); + $this->assertArrayHasKey('third', $request->getErrors()['second']); + } + + public function testDataPassed() + { + $serverRequest = new ServerRequest(); + $serverRequest = $serverRequest->withParsedBody([ + 'first' => [ + 'second' => [ + 'third' => '3rd value', + ], + ] + ]); + $this->container->bind(ServerRequestInterface::class, $serverRequest); + + /** @var DemoRequest $request */ + $request = $this->container->get(ThirdDepthRequest::class); + + $this->assertTrue($request->isValid()); + $this->assertArrayHasKey(['city'], $request->getValidator()->getData()['first']['second']); + $this->assertSame('3rd value', $request->getValidator()->getData()['first']['second']['third']); + } +} diff --git a/tests/Http/RequestFilters/ScopingTest.php b/tests/Http/RequestFilters/ScopingTest.php index eaa127179..240e7095a 100644 --- a/tests/Http/RequestFilters/ScopingTest.php +++ b/tests/Http/RequestFilters/ScopingTest.php @@ -35,7 +35,7 @@ public function getValue(string $source, string $name = null) } } - public function withPrefix(string $prefix): InputInterface + public function withPrefix(string $prefix, bool $add = true): InputInterface { return $this; } From 0ea64fc801376993fed23afca0ba448337677b37 Mon Sep 17 00:00:00 2001 From: valentin v / vvval Date: Thu, 28 Sep 2017 13:16:08 +0300 Subject: [PATCH 08/12] PR Add third-level validation rule --- tests/Http/Fixtures/ThirdDepthRequest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Http/Fixtures/ThirdDepthRequest.php b/tests/Http/Fixtures/ThirdDepthRequest.php index 001c9a129..6a3226a5f 100644 --- a/tests/Http/Fixtures/ThirdDepthRequest.php +++ b/tests/Http/Fixtures/ThirdDepthRequest.php @@ -9,4 +9,8 @@ class ThirdDepthRequest extends RequestFilter const SCHEMA = [ 'third' => 'data:third' ]; + + const VALIDATES = [ + 'third' => ['notEmpty'] + ]; } \ No newline at end of file From 9658c3828b2f036b625a39789319fc6a7b723f0a Mon Sep 17 00:00:00 2001 From: valentin v / vvval Date: Thu, 28 Sep 2017 13:29:53 +0300 Subject: [PATCH 09/12] PR Add third-level validation rule --- .../Http/RequestFilters/MultipleDepthRequestTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/Http/RequestFilters/MultipleDepthRequestTest.php b/tests/Http/RequestFilters/MultipleDepthRequestTest.php index a74807e49..252c4d8c0 100644 --- a/tests/Http/RequestFilters/MultipleDepthRequestTest.php +++ b/tests/Http/RequestFilters/MultipleDepthRequestTest.php @@ -1,4 +1,5 @@ assertFalse($request->isValid()); $this->assertArrayHasKey('first', $request->getErrors()); $this->assertArrayHasKey('second', $request->getErrors()['first']); - $this->assertArrayHasKey('third', $request->getErrors()['second']); + $this->assertArrayHasKey('third', $request->getErrors()['first']['second']); } public function testDataPassed() @@ -41,7 +42,13 @@ public function testDataPassed() $request = $this->container->get(ThirdDepthRequest::class); $this->assertTrue($request->isValid()); - $this->assertArrayHasKey(['city'], $request->getValidator()->getData()['first']['second']); + $this->assertArrayNotHasKey('first', $request->getErrors()); + $this->assertArrayNotHasKey('second', $request->getErrors()['first']); + $this->assertArrayNotHasKey('third', $request->getErrors()['first']['second']); + + $this->assertArrayHasKey('first', $request->getValidator()->getData()); + $this->assertArrayHasKey('second', $request->getValidator()->getData()['first']); + $this->assertArrayHasKey('third', $request->getValidator()->getData()['first']['second']); $this->assertSame('3rd value', $request->getValidator()->getData()['first']['second']['third']); } } From e81a1f45b6f0bd0b33375e761c54c73b5a93d218 Mon Sep 17 00:00:00 2001 From: valentin v / vvval Date: Thu, 28 Sep 2017 13:45:41 +0300 Subject: [PATCH 10/12] PR Add third-level validation rule --- tests/Http/RequestFilters/MultipleDepthRequestTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Http/RequestFilters/MultipleDepthRequestTest.php b/tests/Http/RequestFilters/MultipleDepthRequestTest.php index 252c4d8c0..9b867dcbc 100644 --- a/tests/Http/RequestFilters/MultipleDepthRequestTest.php +++ b/tests/Http/RequestFilters/MultipleDepthRequestTest.php @@ -39,7 +39,7 @@ public function testDataPassed() $this->container->bind(ServerRequestInterface::class, $serverRequest); /** @var DemoRequest $request */ - $request = $this->container->get(ThirdDepthRequest::class); + $request = $this->container->get(FirstDepthRequest::class); $this->assertTrue($request->isValid()); $this->assertArrayNotHasKey('first', $request->getErrors()); From 0a6ca59b923b9fa738c8f5d31609bac03527227b Mon Sep 17 00:00:00 2001 From: valentin v / vvval Date: Thu, 28 Sep 2017 13:59:13 +0300 Subject: [PATCH 11/12] PR Add third-level validation rule --- tests/Http/RequestFilters/MultipleDepthRequestTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Http/RequestFilters/MultipleDepthRequestTest.php b/tests/Http/RequestFilters/MultipleDepthRequestTest.php index 9b867dcbc..3278add90 100644 --- a/tests/Http/RequestFilters/MultipleDepthRequestTest.php +++ b/tests/Http/RequestFilters/MultipleDepthRequestTest.php @@ -43,8 +43,6 @@ public function testDataPassed() $this->assertTrue($request->isValid()); $this->assertArrayNotHasKey('first', $request->getErrors()); - $this->assertArrayNotHasKey('second', $request->getErrors()['first']); - $this->assertArrayNotHasKey('third', $request->getErrors()['first']['second']); $this->assertArrayHasKey('first', $request->getValidator()->getData()); $this->assertArrayHasKey('second', $request->getValidator()->getData()['first']); From e99fb849055f25e49fc746d68430015b13c5ed43 Mon Sep 17 00:00:00 2001 From: Valentin V Date: Thu, 28 Sep 2017 14:38:53 +0300 Subject: [PATCH 12/12] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c4ecfbc7..80853e93c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ CHANGELOG for 0.9.0 RC ====================== +1.0.6 (28.09.2017) +----- +- Fixed a bug when error message was converted to empty array +- Fixed a bug when multidepth requests contained invalid prefix (in case of depth more than 3) + 1.0.5 ----- - ability to locate view cache files by view name and namespace