diff --git a/app/src/Providers/TwigProvider.php b/app/src/Providers/TwigProvider.php index d9586a9..c5af2ed 100644 --- a/app/src/Providers/TwigProvider.php +++ b/app/src/Providers/TwigProvider.php @@ -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) diff --git a/app/src/ViewFunctions/Asset.php b/app/src/ViewFunctions/Asset.php index 5d31957..36cae89 100644 --- a/app/src/ViewFunctions/Asset.php +++ b/app/src/ViewFunctions/Asset.php @@ -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. * diff --git a/app/src/ViewFunctions/Breadcrumbs.php b/app/src/ViewFunctions/Breadcrumbs.php index c1809bd..94188ed 100644 --- a/app/src/ViewFunctions/Breadcrumbs.php +++ b/app/src/ViewFunctions/Breadcrumbs.php @@ -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. * diff --git a/app/src/ViewFunctions/Config.php b/app/src/ViewFunctions/Config.php index bdecf9b..625ad22 100644 --- a/app/src/ViewFunctions/Config.php +++ b/app/src/ViewFunctions/Config.php @@ -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. * diff --git a/app/src/ViewFunctions/Icon.php b/app/src/ViewFunctions/Icon.php index 721eb93..e6085f6 100644 --- a/app/src/ViewFunctions/Icon.php +++ b/app/src/ViewFunctions/Icon.php @@ -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. * diff --git a/app/src/ViewFunctions/ViewFunction.php b/app/src/ViewFunctions/ViewFunction.php index 0ade628..99dd404 100644 --- a/app/src/ViewFunctions/ViewFunction.php +++ b/app/src/ViewFunctions/ViewFunction.php @@ -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. * diff --git a/tests/Exceptions/ErrorHandlerTest.php b/tests/Exceptions/ErrorHandlerTest.php index fbc609d..751445a 100644 --- a/tests/Exceptions/ErrorHandlerTest.php +++ b/tests/Exceptions/ErrorHandlerTest.php @@ -1,6 +1,6 @@ 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' => '']); diff --git a/tests/Providers/TwigProviderTest.php b/tests/Providers/TwigProviderTest.php index 44c4e6f..59a13df 100644 --- a/tests/Providers/TwigProviderTest.php +++ b/tests/Providers/TwigProviderTest.php @@ -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); diff --git a/tests/ViewFunctions/ConfigTest.php b/tests/ViewFunctions/ConfigTest.php index 792dc24..1fc6103 100644 --- a/tests/ViewFunctions/ConfigTest.php +++ b/tests/ViewFunctions/ConfigTest.php @@ -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')); } diff --git a/tests/ViewFunctions/IconTest.php b/tests/ViewFunctions/IconTest.php index 96b32db..f48a292 100644 --- a/tests/ViewFunctions/IconTest.php +++ b/tests/ViewFunctions/IconTest.php @@ -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');