Resolve ViewFunction dependencies form the container

This commit is contained in:
Chris Kankiewicz
2020-02-24 10:36:45 -07:00
parent 53689370bd
commit db1dc84e9e
11 changed files with 83 additions and 34 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Providers;
use App\ViewFunctions;
use DI\Container;
use Invoker\CallableResolver;
use PHLAK\Config\Config;
use Slim\Views\Twig;
use Twig\Extension\CoreExtension;
@@ -30,16 +31,24 @@ class TwigProvider
/** @var Config Application config */
protected $config;
/** @var CallableResolver The callable resolver */
protected $callableResolver;
/**
* Create a new ViewProvider object.
*
* @param \DI\Container $container
* @param \PHLAK\Config\Config $config
* @param \DI\Container $container
* @param \PHLAK\Config\Config $config
* @param \Invoker\CallableResolver $callableResolver
*/
public function __construct(Container $container, Config $config)
{
public function __construct(
Container $container,
Config $config,
CallableResolver $callableResolver
) {
$this->container = $container;
$this->config = $config;
$this->callableResolver = $callableResolver;
}
/**
@@ -60,7 +69,7 @@ class TwigProvider
);
foreach (self::VIEW_FUNCTIONS as $function) {
$function = new $function($this->container, $this->config);
$function = $this->callableResolver->resolve($function);
$twig->getEnvironment()->addFunction(
new TwigFunction($function->name(), $function)

View File

@@ -2,6 +2,7 @@
namespace App\ViewFunctions;
use DI\Container;
use Tightenco\Collect\Support\Collection;
class Asset extends ViewFunction
@@ -12,6 +13,19 @@ class Asset extends ViewFunction
/** @var string The function name */
protected $name = 'asset';
/** @var Container The application container */
protected $container;
/**
* Create a new Asset object.
*
* @param \DI\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Return the path to an asset.
*

View File

@@ -2,6 +2,7 @@
namespace App\ViewFunctions;
use DI\Container;
use Tightenco\Collect\Support\Collection;
class Breadcrumbs extends ViewFunction
@@ -9,6 +10,20 @@ class Breadcrumbs extends ViewFunction
/** @var string The function name */
protected $name = 'breadcrumbs';
/** @var Container The application container */
protected $container;
/**
* Create a new Breadcrumbs object.
*
* @param \DI\Container $container
* @param \PHLAK\Config\Config $config
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Build an array of breadcrumbs for a given path.
*

View File

@@ -2,11 +2,26 @@
namespace App\ViewFunctions;
use PHLAK\Config\Config as AppConfig;
class Config extends ViewFunction
{
/** @var string The function name */
protected $name = 'config';
/** @var \PHLAK\Config\Config */
protected $config;
/**
* Create a new Config object.
*
* @param \PHLAK\Config\Config $config
*/
public function __construct(AppConfig $config)
{
$this->config = $config;
}
/**
* Retrieve an item from the view config.
*

View File

@@ -2,6 +2,7 @@
namespace App\ViewFunctions;
use PHLAK\Config\Config;
use Symfony\Component\Finder\SplFileInfo;
class Icon extends ViewFunction
@@ -9,6 +10,19 @@ class Icon extends ViewFunction
/** @var string The function name */
protected $name = 'icon';
/** @var \PHLAK\Config\Config */
protected $config;
/**
* Create a new Config object.
*
* @param \PHLAK\Config\Config $config
*/
public function __construct(Config $config)
{
$this->config = $config;
}
/**
* Retrieve the icon markup for a file.
*

View File

@@ -2,31 +2,11 @@
namespace App\ViewFunctions;
use DI\Container;
use PHLAK\Config\Config;
abstract class ViewFunction
{
/** @var string The function name */
protected $name = '';
/** @var Container The application container */
protected $container;
/** @var Config App configuration component */
protected $config;
/**
* Create a new ViewFunction object.
*
* @param \PHLAK\Config\Config $config
*/
public function __construct(Container $container, Config $config)
{
$this->container = $container;
$this->config = $config;
}
/**
* Get the function name.
*

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Bootstrap;
namespace Tests\Exceptions;
use App\Exceptions\ErrorHandler;
use App\Providers\TwigProvider;

View File

@@ -17,7 +17,7 @@ class SearchHandlerTest extends TestCase
{
$this->container->call(TwigProvider::class);
$handler = new SearchHandler(new Finder, $this->container->get(Twig::class));
$handler = new SearchHandler(new Finder, $this->container->get(Twig::class), $this->translator);
$request = $this->createMock(Request::class);
$request->method('getQueryParams')->willReturn(['search' => 'charlie']);
@@ -32,7 +32,7 @@ class SearchHandlerTest extends TestCase
{
$this->container->call(TwigProvider::class);
$handler = new SearchHandler(new Finder, $this->container->get(Twig::class));
$handler = new SearchHandler(new Finder, $this->container->get(Twig::class), $this->translator);
$request = $this->createMock(Request::class);
$request->method('getQueryParams')->willReturn(['search' => '']);

View File

@@ -4,6 +4,7 @@ namespace Tests\Providers;
use App\Providers\TwigProvider;
use App\ViewFunctions;
use Invoker\CallableResolver;
use PHLAK\Config\Config;
use Slim\Views\Twig;
use Tests\TestCase;
@@ -12,7 +13,8 @@ class TwigProviderTest extends TestCase
{
public function test_it_can_compose_the_view_component(): void
{
(new TwigProvider($this->container, new Config))();
$callableResolver = $this->container->get(CallableResolver::class);
(new TwigProvider($this->container, new Config, $callableResolver))();
$twig = $this->container->get(Twig::class);

View File

@@ -26,14 +26,14 @@ class ConfigTest extends TestCase
public function test_it_can_retrieve_a_config_item(): void
{
$config = new Config($this->container, $this->config);
$config = new Config($this->config);
$this->assertEquals('Test value; please ignore', $config('foo'));
}
public function test_it_returns_a_default_value(): void
{
$config = new Config($this->container, $this->config);
$config = new Config($this->config);
$this->assertEquals('Default value', $config('bar', 'Default value'));
}

View File

@@ -26,7 +26,7 @@ class IconTest extends TestCase
public function test_it_can_return_icon_markup_for_a_file(): void
{
$icon = new Icon($this->container, $this->config);
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(false);
$file->method('getExtension')->willReturn('php');
@@ -36,7 +36,7 @@ class IconTest extends TestCase
public function test_it_can_return_icon_markup_for_a_directory(): void
{
$icon = new Icon($this->container, $this->config);
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(true);
@@ -45,7 +45,7 @@ class IconTest extends TestCase
public function test_it_can_return_the_default_icon_markup(): void
{
$icon = new Icon($this->container, $this->config);
$icon = new Icon($this->config);
$file = $this->createMock(SplFileInfo::class);
$file->method('isDir')->willReturn(false);
$file->method('getExtension')->willReturn('default');