Skip to content

Commit

Permalink
Make sure that necessary Wireframe features are available to Factory …
Browse files Browse the repository at this point in the history
…methods
  • Loading branch information
teppokoivula committed Feb 27, 2022
1 parent 2f2ee26 commit 7fab89c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions lib/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
*/
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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']);
Expand All @@ -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)) {
Expand All @@ -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,
]);
Expand Down

0 comments on commit 7fab89c

Please sign in to comment.