Skip to content
This repository has been archived by the owner on Aug 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from ncou/analysis-8wLGbw
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
ncou committed Oct 13, 2018
2 parents 199d86d + a5cd160 commit a970ed8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
62 changes: 38 additions & 24 deletions src/FileViewFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class FileViewFinder
/** Identifier of the default namespace. */
public const DEFAULT_NAMESPACE = '__DEFAULT__';

protected $paths = array();
protected $paths = [];

protected $cache = array();
protected $cache = [];

/**
* Namespace path delimiter value.
Expand All @@ -26,12 +26,12 @@ class FileViewFinder
* @var array
*/
protected $extensions = ['phtml', 'html', 'php'];

/**
* Create a new file view loader instance.
*
* @param array $paths
* @param array $extensions
* @return void
* @param array $paths
* @param array $extensions
*/
public function __construct(array $paths = [], array $extensions = null)
{
Expand All @@ -46,7 +46,8 @@ public function __construct(array $paths = [], array $extensions = null)
/**
* Determine if a given view exists.
*
* @param string $name
* @param string $name
*
* @return bool
*/
public function exists(string $name): bool
Expand All @@ -60,13 +61,15 @@ public function exists(string $name): bool
} catch (\InvalidArgumentException $e) {
return false;
}

return true;
}

/**
* Get the fully qualified location of the view.
*
* @param string $name
* @param string $name
*
* @return string
*/
public function findTemplate(string $name): string
Expand All @@ -79,20 +82,24 @@ public function findTemplate(string $name): string

foreach ($this->paths[$namespace] as $path) {
foreach ($this->getPossibleViewFiles($shortname) as $file) {
if (is_file($viewPath = $path.'/'.$file)) {
if (is_file($viewPath = $path . '/' . $file)) {
if (false !== $realpath = realpath($viewPath)) {
return $this->cache[$name] = $realpath;
}

return $this->cache[$name] = $viewPath;
}
}
}

throw new \InvalidArgumentException("Template [{$name}] not found.");
}

/**
* Get the path to a template with a named path.
*
* @param string $name
* @param string $name
*
* @return array
*/
protected function parseName(string $name, string $default = self::DEFAULT_NAMESPACE): array
Expand All @@ -101,15 +108,17 @@ protected function parseName(string $name, string $default = self::DEFAULT_NAMES
return $this->parseNamespaceSegments($name);
}

return array($default, $name);
return [$default, $name];
}

/**
* Get the segments of a template with a named path.
*
* @param string $name
* @return array
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return array
*/
protected function parseNamespaceSegments(string $name): array
{
Expand All @@ -120,22 +129,24 @@ protected function parseNamespaceSegments(string $name): array
if (! isset($this->paths[$segments[0]])) {
throw new \InvalidArgumentException("No namespace defined for [{$segments[0]}].");
}

return $segments;
}

/**
* Get an array of possible view files.
*
* @param string $name
* @param string $name
*
* @return array
*/
protected function getPossibleViewFiles(string $name): array
{
return array_map(function ($extension) use ($name) {
return str_replace('.', '/', $name).'.'.$extension;
return str_replace('.', '/', $name) . '.' . $extension;
}, $this->extensions);
}


/**
* Returns the paths to the templates.
*
Expand All @@ -145,8 +156,9 @@ protected function getPossibleViewFiles(string $name): array
*/
public function getPaths($namespace = self::DEFAULT_NAMESPACE)
{
return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
return isset($this->paths[$namespace]) ? $this->paths[$namespace] : [];
}

/**
* Returns the path namespaces.
*
Expand All @@ -158,6 +170,7 @@ public function getNamespaces()
{
return array_keys($this->paths);
}

/**
* Sets the paths where templates are stored.
*
Expand All @@ -166,10 +179,10 @@ public function getNamespaces()
*/
public function setPaths($paths, $namespace = self::DEFAULT_NAMESPACE)
{
if (!is_array($paths)) {
$paths = array($paths);
if (! is_array($paths)) {
$paths = [$paths];
}
$this->paths[$namespace] = array();
$this->paths[$namespace] = [];
foreach ($paths as $path) {
$this->addPath($path, $namespace);
}
Expand All @@ -186,16 +199,15 @@ public function setPaths($paths, $namespace = self::DEFAULT_NAMESPACE)
public function addPath(string $path, string $namespace = self::DEFAULT_NAMESPACE)
{
// invalidate the cache
$this->cache = array();
$this->cache = [];

$this->paths[$namespace][] = rtrim($path, '/\\');
}

/**
* Register an extension with the view finder.
*
* @param string $extension
* @return void
* @param string $extension
*/
public function addExtension(string $extension): void
{
Expand All @@ -204,6 +216,7 @@ public function addExtension(string $extension): void
}
array_unshift($this->extensions, $extension);
}

/**
* Get registered extensions.
*
Expand All @@ -213,15 +226,16 @@ public function getExtensions(): array
{
return $this->extensions;
}

/**
* Returns whether or not the view name has any hint information.
*
* @param string $name
* @param string $name
*
* @return bool
*/
public function hasHintInformation(string $name): bool
{
return strpos($name, static::NAMESPACE_DELIMITER) > 0;
}

}
4 changes: 3 additions & 1 deletion src/PhpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function render(string $sourceFile, array $variables = []): string
$content = ob_get_clean();
} catch (\Throwable $e) {
ob_end_clean();

throw $e;
}

return $content;
}
}
}
29 changes: 13 additions & 16 deletions src/PhpRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

namespace Chiron\Views;

use Chiron\Views\AttributesTrait;
use Chiron\Views\FileViewFinder;
use Chiron\Views\TemplateRendererInterface;
use Chiron\Views\TemplatePath;

class PhpRenderer implements TemplateRendererInterface
{
use AttributesTrait;

private $engine;

private $finder;

public function __construct()
Expand All @@ -29,33 +25,34 @@ public function __construct()
* and allow omitting the filename extension.
*
* @param string $name
* @param array $params
* @param array $params
*/
public function render(string $name, array $params = []) : string
public function render(string $name, array $params = []): string
{
$path = $this->finder->findTemplate($name);
$params = array_merge($this->attributes, $params);

return $this->engine->render($path, $params);
}

/**
* Add a template path to the engine.
*
* Adds a template path, with optional namespace the templates in that path
* provide.
*/
public function addPath(string $path, string $namespace = null) : void
public function addPath(string $path, string $namespace = null): void
{
$namespace = $namespace ?: FileViewFinder::DEFAULT_NAMESPACE;
$this->finder->addPath($path, $namespace);
}

/**
* Get the template directories
* Get the template directories.
*
* @return TemplatePath[]
*/
public function getPaths() : array
public function getPaths(): array
{
$paths = [];
foreach ($this->finder->getNamespaces() as $namespace) {
Expand All @@ -64,11 +61,12 @@ public function getPaths() : array
$paths[] = new TemplatePath($path, $name);
}
}

return $paths;
}

/**
* Get the template directories
* Get the template directories.
*
* @return TemplatePath[]
*/
Expand All @@ -92,20 +90,19 @@ public function getPaths() : array
return $templatePaths;
}*/


/**
* Checks if the view exists.
*
* @param string $name View name
* @param string $name View name
*
* @return bool True if the path exists
* @return bool True if the path exists
*/
public function exists(string $name): bool
{
return $this->finder->exists($name);
}

/**
/*
* Wrapping method to redirect methods not available in this class to the
* internal instance of the Finder class used for the rendering engine.
* @param string $name Unknown method to call in the internal Twig rendering engine.
Expand All @@ -118,7 +115,7 @@ public function __call($name, $arguments)
call_user_func_array(array($this->finder, $name), $arguments);
}*/

/**
/*
* Wrapping method to redirect static methods not available in this class
* to the internal instance of the Twig rendering engine.
* @param string $name Unknown static method to call in the internal Twig rendering engine.
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/PhpRendererServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Chiron\Views\Provider;

use Psr\Container\ContainerInterface;
use Chiron\Views\TemplateRendererInterface;
use Chiron\Views\PhpRenderer;
use Chiron\Views\TemplateRendererInterface;
use Psr\Container\ContainerInterface;

class PhpRendererServiceProvider
{
Expand Down
10 changes: 7 additions & 3 deletions tests/PhpRendererTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

use PHPUnit\Framework\TestCase;

use Chiron\Views\PhpRenderer;
use Chiron\Views\TemplatePath;
use PHPUnit\Framework\TestCase;

class PhpRendererTest extends TestCase
{
Expand All @@ -12,17 +11,20 @@ public function assertTemplatePath($path, TemplatePath $templatePath, $message =
$message = $message ?: sprintf('Failed to assert TemplatePath contained path %s', $path);
$this->assertEquals($path, $templatePath->getPath(), $message);
}

public function assertTemplatePathString($path, TemplatePath $templatePath, $message = null)
{
$message = $message ?: sprintf('Failed to assert TemplatePath casts to string path %s', $path);
$this->assertEquals($path, (string) $templatePath, $message);
}

public function assertTemplatePathNamespace($namespace, TemplatePath $templatePath, $message = null)
{
$message = $message
?: sprintf('Failed to assert TemplatePath namespace matched %s', var_export($namespace, true));
$this->assertEquals($namespace, $templatePath->getNamespace(), $message);
}

public function assertEmptyTemplatePathNamespace(TemplatePath $templatePath, $message = null)
{
$message = $message ?: 'Failed to assert TemplatePath namespace was empty';
Expand All @@ -40,6 +42,7 @@ public function testCanAddPathWithEmptyNamespace()
$this->assertTemplatePathString(__DIR__ . '/TestAsset', $paths[0]);
$this->assertEmptyTemplatePathNamespace($paths[0]);
}

public function testCanAddPathWithNamespace()
{
$renderer = new PhpRenderer();
Expand All @@ -51,11 +54,12 @@ public function testCanAddPathWithNamespace()
$this->assertTemplatePathString(__DIR__ . '/TestAsset', $paths[0]);
$this->assertTemplatePathNamespace('test', $paths[0]);
}

public function testDelegatesRenderingToUnderlyingImplementation()
{
$renderer = new PhpRenderer();
$renderer->addPath(__DIR__ . '/TestAsset');
$result = $renderer->render('testTemplate', [ 'hello' => 'Hi' ]);
$result = $renderer->render('testTemplate', ['hello' => 'Hi']);
$this->assertEquals('Hi', $result);
}

Expand Down

0 comments on commit a970ed8

Please sign in to comment.