-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that necessary Wireframe features are available to Factory …
…methods
- Loading branch information
1 parent
2f2ee26
commit 7fab89c
Showing
1 changed file
with
30 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,19 @@ | |
|
||
namespace Wireframe; | ||
|
||
use ProcessWire\{HookEvent, NullPage, Page, WireException, Wireframe}; | ||
use ProcessWire\ProcessWire; | ||
use ProcessWire\HookEvent; | ||
use ProcessWire\NullPage; | ||
use ProcessWire\Page; | ||
use ProcessWire\WireException; | ||
use ProcessWire\Wireframe; | ||
|
||
use function ProcessWire\wire; | ||
|
||
/** | ||
* Factory class for Wireframe | ||
* | ||
* @version 0.2.1 | ||
* @version 0.2.2 | ||
* @author Teppo Koivula <[email protected]> | ||
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
@@ -53,6 +59,11 @@ private function __construct() {} | |
*/ | ||
public static function component(string $component_name, array $args = []): \Wireframe\Component { | ||
|
||
// make sure that basic Wireframe features have been initialized | ||
if (!Wireframe::isInitialized(wire()->instanceID)) { | ||
wire()->modules->get('Wireframe')->initOnce(); | ||
} | ||
|
||
$component_class = '\Wireframe\Component\\' . $component_name; | ||
|
||
if (!class_exists($component_class)) { | ||
|
@@ -150,9 +161,14 @@ public static function component(string $component_name, array $args = []): \Wir | |
*/ | ||
public static function page($source, $args = []) { | ||
|
||
// ProcessWire instance | ||
/** @var ProcessWire */ | ||
$wire = $args['wire'] ?? ($source instanceof Page ? $source->getWire() : wire()); | ||
|
||
// make sure that basic Wireframe features have been initialized | ||
if (!Wireframe::isInitialized($wire->instanceID)) { | ||
($args['wireframe'] ?? $wire->modules->get('Wireframe'))->initOnce(); | ||
} | ||
|
||
// get a page | ||
$page = null; | ||
if ($source instanceof Page) { | ||
|
@@ -238,11 +254,6 @@ public static function page($source, $args = []) { | |
} | ||
} | ||
|
||
// make sure that basic Wireframe features have been intiialized | ||
if (!Wireframe::isInitialized($wire->instanceID)) { | ||
($args['wireframe'] ?? $wire->modules->get('Wireframe'))->initOnce(); | ||
} | ||
|
||
// set view, layout, and view template | ||
if ($args['layout'] != 'default') $page->setLayout($args['layout']); | ||
if ($args['view'] != 'default') $page->setView($args['view']); | ||
|
@@ -264,12 +275,21 @@ public static function page($source, $args = []) { | |
* @throws WireException if partials path isn't found from config. | ||
*/ | ||
public static function partial(string $partial_name, array $args = null) { | ||
|
||
// validate partial name | ||
if (\strpos($partial_name, '..') !== false) { | ||
throw new WireException(sprintf( | ||
'Partial name is invalid (%s)', | ||
$partial_name | ||
)); | ||
} | ||
|
||
// make sure that basic Wireframe features have been initialized | ||
if (!Wireframe::isInitialized(wire()->instanceID)) { | ||
wire()->modules->get('Wireframe')->initOnce(); | ||
} | ||
|
||
// get path and ext for partial | ||
$config = wire('config'); | ||
$partials_path = $config->paths->partials; | ||
if (empty($partials_path)) { | ||
|
@@ -289,6 +309,8 @@ public static function partial(string $partial_name, array $args = null) { | |
} | ||
} | ||
} | ||
|
||
// instantiate and return/render partial | ||
$partial = new Partial([ | ||
\ltrim($ext, '.') => $partials_path . $partial_name . $ext, | ||
]); | ||
|