ViewServiceProvider::bootstrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
nc 1
nop 1
dl 0
loc 1
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\View;
11
12
use Pimple\Container;
13
use WPEmerge\Helpers\MixedType;
14
use WPEmerge\ServiceProviders\ExtendsConfigTrait;
15
use WPEmerge\ServiceProviders\ServiceProviderInterface;
16
17
/**
18
 * Provide view dependencies
19
 *
20
 * @codeCoverageIgnore
21
 */
22
class ViewServiceProvider implements ServiceProviderInterface {
23
	use ExtendsConfigTrait;
24
25
	/**
26
	 * {@inheritDoc}
27
	 */
28
	public function register( $container ) {
29
		/** @var Container $container */
30
		$namespace = $container[ WPEMERGE_CONFIG_KEY ]['namespace'];
31
32
		$this->extendConfig( $container, 'views', [get_stylesheet_directory(), get_template_directory()] );
33
34
		$this->extendConfig( $container, 'view_composers', [
35
			'namespace' => $namespace . 'ViewComposers\\',
36
		] );
37
38
		$container[ WPEMERGE_VIEW_SERVICE_KEY ] = function ( $c ) {
39
			return new ViewService(
40
				$c[ WPEMERGE_CONFIG_KEY ]['view_composers'],
41
				$c[ WPEMERGE_VIEW_ENGINE_KEY ],
42
				$c[ WPEMERGE_HELPERS_HANDLER_FACTORY_KEY ]
43
			);
44
		};
45
46
		$container[ WPEMERGE_VIEW_COMPOSE_ACTION_KEY ] = function ( $c ) {
47
			return function ( ViewInterface $view ) use ( $c ) {
48
				$view_service = $c[ WPEMERGE_VIEW_SERVICE_KEY ];
49
				$view_service->compose( $view );
50
				return $view;
51
			};
52
		};
53
54
		$container[ WPEMERGE_VIEW_PHP_VIEW_ENGINE_KEY ] = function ( $c ) {
55
			$finder = new PhpViewFilesystemFinder( MixedType::toArray( $c[ WPEMERGE_CONFIG_KEY ]['views'] ) );
56
			return new PhpViewEngine( $c[ WPEMERGE_VIEW_COMPOSE_ACTION_KEY ], $finder );
57
		};
58
59
		$container[ WPEMERGE_VIEW_ENGINE_KEY ] = function ( $c ) {
60
			return $c[ WPEMERGE_VIEW_PHP_VIEW_ENGINE_KEY ];
61
		};
62
63
		$app = $container[ WPEMERGE_APPLICATION_KEY ];
64
		$app->alias( 'views', WPEMERGE_VIEW_SERVICE_KEY );
65
66
		$app->alias( 'view', function () use ( $app ) {
67
			return call_user_func_array( [$app->views(), 'make'], func_get_args() );
68
		} );
69
70
		$app->alias( 'render', function () use ( $app ) {
71
			return call_user_func_array( [$app->views(), 'render'], func_get_args() );
72
		} );
73
74
		$app->alias( 'layoutContent', function () use ( $app ) {
75
			/** @var PhpViewEngine $engine */
76
			$engine = $app->resolve( WPEMERGE_VIEW_PHP_VIEW_ENGINE_KEY );
77
78
			echo $engine->getLayoutContent();
79
		} );
80
	}
81
82
	/**
83
	 * {@inheritDoc}
84
	 */
85
	public function bootstrap( $container ) {
86
		// Nothing to bootstrap.
87
	}
88
}
89