Skip to content

Commit

Permalink
Merge pull request #197 from spiral/develop
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
wolfy-j authored Sep 29, 2017
2 parents d57007e + e99fb84 commit 75560b7
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 13 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
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

1.0.4
-----
- ability to add conditions to skip validation rules

1.0.2 (04.05.2017)
-----
Expand Down
4 changes: 3 additions & 1 deletion source/Spiral/Http/Request/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 8 additions & 2 deletions source/Spiral/Http/Request/InputManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions source/Spiral/Http/Request/InputMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -264,4 +264,4 @@ private function createOrigins(

return $result;
}
}
}
32 changes: 32 additions & 0 deletions source/Spiral/Views/AbstractViewCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* (c) Lev Seleznev (tuneyourserver) 2017
*/

namespace Spiral\Views;

/**
* Class AbstractViewCache
*
* Provides ability to locate cache files by common prefix with many environments.
*
* @package Spiral\Views
*/
abstract class AbstractViewCache
{
/**
* Returns file name prefix by namespace:name
*
* @param string $name
* @param string $namespace
* @return string
*/
protected function getPrefix($name, $namespace = 'default')
{
$prefix = $namespace . ViewManager::NS_SEPARATOR . $name;
$prefix = preg_replace('/([^A-Za-z0-9]|-)+/', '-', $prefix) . '-' .
hash('md5', $name . ViewManager::NS_SEPARATOR . $namespace);

return $prefix;
}
}
6 changes: 4 additions & 2 deletions source/Spiral/Views/Engines/Stempler/StemplerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
namespace Spiral\Views\Engines\Stempler;

use Spiral\Files\FilesInterface;
use Spiral\Views\AbstractViewCache;
use Spiral\Views\EnvironmentInterface;
use Spiral\Views\ViewSource;

/**
* Very simple Stempler cache. Almost identical to twig cache except generateKey method.
*/
class StemplerCache
class StemplerCache extends AbstractViewCache
{
/**
* @var FilesInterface
Expand Down Expand Up @@ -61,7 +62,8 @@ public function cacheFilename(ViewSource $context): string
{
$hash = hash('md5', $context->getFilename() . '.' . $this->environment->getID());

return $this->environment->cacheDirectory() . $hash . '.php';
return $this->environment->cacheDirectory() .
$this->getPrefix($context->getName(), $context->getNamespace()) . '-' . $hash . '.php';
}

/**
Expand Down
6 changes: 4 additions & 2 deletions source/Spiral/Views/Engines/Twig/TwigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
}

/**
Expand Down
52 changes: 52 additions & 0 deletions source/Spiral/Views/ViewCacheLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* (c) Lev Seleznev (tuneyourserver) 2017
*/

namespace Spiral\Views;


use Spiral\Files\FileManager;
use Spiral\Views\Configs\ViewsConfig;

/**
* Class ViewCacheLocator
*
* Provides ability to locate cache files by view name and namespace
*
* @package Spiral\Views
*/
class ViewCacheLocator extends AbstractViewCache
{
/** @var FileManager */
protected $fileManager;

/** @var ViewsConfig */
protected $config;

/**
* CacheLocator constructor.
*
* @param ViewsConfig $config
* @param FileManager $fileManager
*/
public function __construct(ViewsConfig $config, FileManager $fileManager)
{
$this->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.'-*');
}
}
12 changes: 12 additions & 0 deletions tests/Http/Fixtures/FirstDepthRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spiral\Tests\Http\Fixtures;

use Spiral\Http\Request\RequestFilter;

class FirstDepthRequest extends RequestFilter
{
const SCHEMA = [
'first' => SecondDepthRequest::class
];
}
12 changes: 12 additions & 0 deletions tests/Http/Fixtures/SecondDepthRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spiral\Tests\Http\Fixtures;

use Spiral\Http\Request\RequestFilter;

class SecondDepthRequest extends RequestFilter
{
const SCHEMA = [
'second' => ThirdDepthRequest::class
];
}
16 changes: 16 additions & 0 deletions tests/Http/Fixtures/ThirdDepthRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spiral\Tests\Http\Fixtures;

use Spiral\Http\Request\RequestFilter;

class ThirdDepthRequest extends RequestFilter
{
const SCHEMA = [
'third' => 'data:third'
];

const VALIDATES = [
'third' => ['notEmpty']
];
}
52 changes: 52 additions & 0 deletions tests/Http/RequestFilters/MultipleDepthRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Spiral\Tests\Http\RequestFilters;

use Psr\Http\Message\ServerRequestInterface;
use Spiral\Tests\Http\Fixtures\DemoRequest;
use Spiral\Tests\Http\Fixtures\FirstDepthRequest;
use Spiral\Tests\Http\Fixtures\ThirdDepthRequest;
use Spiral\Tests\Http\HttpTest;
use Zend\Diactoros\ServerRequest;

class MultipleDepthRequestTest extends HttpTest
{
public function testHasErrors()
{
$serverRequest = new ServerRequest();
$serverRequest = $serverRequest->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()['first']['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(FirstDepthRequest::class);

$this->assertTrue($request->isValid());
$this->assertArrayNotHasKey('first', $request->getErrors());

$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']);
}
}
2 changes: 1 addition & 1 deletion tests/Http/RequestFilters/ScopingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/Translator/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 38 additions & 2 deletions tests/Views/StemplerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -53,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'
]));

Expand All @@ -64,7 +77,7 @@ public function testRenderFromOtherLoader()
)
);

$this->assertSame('home alt', $views->render('home'));
$this->assertContains('home alt', $views->render('home'));
}

public function testRenderFromCache()
Expand Down Expand Up @@ -92,4 +105,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')));
}
}

0 comments on commit 75560b7

Please sign in to comment.