class ViewFactory

View factory.

Traits

Namespaced file loader trait.

Properties

protected string $path Path.
protected string $extension File extension. from NamespacedFileLoaderTrait
protected array $namespaces Namespaces. from NamespacedFileLoaderTrait
protected FileSystem $fileSystem File sytem.
protected string $charset Charset.
protected Container $container Container.
protected array $renderers View renderers.
protected array $globalVariables Global view variables.
protected array $autoAssignVariables Variables that should be auto assigned to views.
protected array $viewCache View cache.
protected array $rendererInstances Renderer instances.

Methods

setPath(string $path)

Sets the default path.

setExtension(string $extension)

Sets the extension.

registerNamespace(string $namespace, string $path)

Registers a namespace.

string
getFilePath(string $file, string|null $extension = null, string|null $suffix = null)

Returns the path to the file.

array
getCascadingFilePaths(string $file, string|null $extension = null, string|null $suffix = null)

Returns an array of cascading file paths.

__construct(FileSystem $fileSystem, string $path, string $charset = 'UTF-8', Container|null $container = null)

Constructor.

string
getCharset()

Returns the charset.

setCharset(string $charset)

Sets the charset.

extend(string $extension, string|Closure $renderer)

Registers a custom view renderer.

assign(string $name, mixed $value)

Assign a global view variable that will be available in all views.

autoAssign(string $view, callable $variables)

Assign variables that should be auto assigned to views upon creation.

clearAutoAssignVariables()

Clears the autoassign variables.

array|bool
getViewPathAndExtension(string $view, bool $throwException = true)

Returns an array containing the view path and the renderer we should use.

rendererFactory(string|Closure $renderer)

Creates a renderer instance.

resolveRenderer(string $extension)

Returns a renderer instance.

bool
exists(string $view)

Returns TRUE if the view exists and FALSE if not.

array
getAutoAssignVariablesForView(string $view)

Returns view specific auto assign variables.

array
getAutoAssignVariables(string $view)

Returns auto assign variables for a view.

array
mergeVariables(string $view, array $variables)

Returns array where variables have been merged in order of importance.

create(string $view, array $variables = [])

Creates and returns a view instance.

string
render(string $view, array $variables = [])

Creates and returns a rendered view.

Details

setPath(string $path)

Sets the default path.

Parameters

string $path Path

setExtension(string $extension)

Sets the extension.

Parameters

string $extension Extension

registerNamespace(string $namespace, string $path)

Registers a namespace.

Parameters

string $namespace Namespace name
string $path Namespace path

protected string getFilePath(string $file, string|null $extension = null, string|null $suffix = null)

Returns the path to the file.

Parameters

string $file Filename
string|null $extension File extension
string|null $suffix Path suffix

Return Value

string

protected array getCascadingFilePaths(string $file, string|null $extension = null, string|null $suffix = null)

Returns an array of cascading file paths.

Parameters

string $file Filename
string|null $extension File extension
string|null $suffix Path suffix

Return Value

array

at line 99
__construct(FileSystem $fileSystem, string $path, string $charset = 'UTF-8', Container|null $container = null)

Constructor.

Parameters

FileSystem $fileSystem File system instance
string $path Default path
string $charset Charset
Container|null $container Container

at line 117
string getCharset()

Returns the charset.

Return Value

string

at line 128
ViewFactory setCharset(string $charset)

Sets the charset.

Parameters

string $charset Charset

Return Value

ViewFactory

at line 142
ViewFactory extend(string $extension, string|Closure $renderer)

Registers a custom view renderer.

Parameters

string $extension Extension handled by the renderer
string|Closure $renderer Renderer class or closure that creates a renderer instance

Return Value

ViewFactory

at line 156
ViewFactory assign(string $name, mixed $value)

Assign a global view variable that will be available in all views.

Parameters

string $name Variable name
mixed $value View variable

Return Value

ViewFactory

at line 170
ViewFactory autoAssign(string $view, callable $variables)

Assign variables that should be auto assigned to views upon creation.

Parameters

string $view View name or array of view names
callable $variables Callable that returns an array of variables

Return Value

ViewFactory

at line 185
ViewFactory clearAutoAssignVariables()

Clears the autoassign variables.

Return Value

ViewFactory

at line 199
protected array|bool getViewPathAndExtension(string $view, bool $throwException = true)

Returns an array containing the view path and the renderer we should use.

Parameters

string $view View
bool $throwException Throw exception if view doesn't exist?

Return Value

array|bool

at line 237
protected RendererInterface rendererFactory(string|Closure $renderer)

Creates a renderer instance.

Parameters

string|Closure $renderer Renderer class or closure

Return Value

RendererInterface

at line 248
protected RendererInterface resolveRenderer(string $extension)

Returns a renderer instance.

Parameters

string $extension Extension associated with the renderer

Return Value

RendererInterface

at line 264
bool exists(string $view)

Returns TRUE if the view exists and FALSE if not.

Parameters

string $view View

Return Value

bool

at line 275
protected array getAutoAssignVariablesForView(string $view)

Returns view specific auto assign variables.

Parameters

string $view View

Return Value

array

at line 291
protected array getAutoAssignVariables(string $view)

Returns auto assign variables for a view.

Parameters

string $view View

Return Value

array

at line 303
protected array mergeVariables(string $view, array $variables)

Returns array where variables have been merged in order of importance.

Parameters

string $view View
array $variables View variables

Return Value

array

at line 315
View create(string $view, array $variables = [])

Creates and returns a view instance.

Parameters

string $view View
array $variables View variables

Return Value

View

at line 329
string render(string $view, array $variables = [])

Creates and returns a rendered view.

Parameters

string $view View
array $variables View variables

Return Value

string