Skip to content

Commit 49f616b

Browse files
author
Wazabii
committed
Improvements
1 parent 7b2a634 commit 49f616b

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

Dom/Document.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,22 @@ public function get(): string
6262
public static function dom(string $key): self
6363
{
6464
if (empty(self::$inst[$key])) {
65-
self::$inst[$key] = new self();
65+
self::$inst[$key] = self::withDom($key);
6666
}
6767
return self::$inst[$key];
6868
}
6969

70+
/**
71+
* Init DOM instance
72+
* @param string $key DOM access key
73+
* @return self
74+
*/
75+
public static function withDom(string $key): self
76+
{
77+
self::$inst[$key] = new self();
78+
return self::$inst[$key];
79+
}
80+
7081
/**
7182
* Create and bind tag to a key so it can be overwritten
7283
* @param string $tag HTML tag (without brackets)

SwiftRender.php

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,29 +223,22 @@ public function withView(string $file, array $args = array()): self
223223
*/
224224
public function setPartial(string $partial, array $args = array()): self
225225
{
226-
227-
$file = $key2 = $key = $partial;
228-
229-
$partial = explode(".", $file);
230-
if(count($partial) > 1) {
231-
$key2 = $file = $partial[1];
232-
$key = $partial[0];
233-
234-
if(isset($partial[2])) {
235-
$key2 = "{$key}-{$partial[2]}";
236-
237-
}
238-
}
239-
226+
$keys = $this->selectPartial($partial, $file);
240227
if (is_null($this->file)) {
241228
$this->setFile($file);
242229
}
243230
$func = $this->build($this->file, $args);
244-
$this->partial[$key][$key2] = $func;
231+
$this->partial[$keys[0]][$keys[1]] = $func;
245232

246233
return $this;
247234
}
248235

236+
public function hasPartial($partial): bool
237+
{
238+
$keys = $this->selectPartial($partial);
239+
return isset($this->partial[$keys[0]][$keys[1]]);
240+
}
241+
249242
/**
250243
* Unset a setted partial
251244
* @param string $key Partal key, example: ("sidebar", "breadcrumb")
@@ -406,10 +399,8 @@ private function buildView(): void
406399
*/
407400
private function build(string|callable $file, array $args = array()): callable
408401
{
409-
410402
$this->arguments = $args;
411403
$func = function ($argsFromFile) use ($file, $args) {
412-
413404
if (($dir = ($this->dir[$this->get] ?? null)) || !is_null($dir)) {
414405
if (is_callable($file)) {
415406
$out = $file($this, $args);
@@ -523,4 +514,44 @@ public function isEl($elem): bool
523514
{
524515
return ($elem instanceof Element);
525516
}
517+
518+
519+
/**
520+
* Get partial keys
521+
* @param string $partialKey
522+
* @param string|null &$file
523+
* @return array
524+
*/
525+
final protected function selectPartial(string $partialKey, ?string &$file = null): array
526+
{
527+
$key = $partialKey;
528+
$partial = explode(".", $partialKey);
529+
$file = $key2 = $key = $this->cleanKey($key);
530+
531+
if (count($partial) > 1) {
532+
$key2 = $partial[1];
533+
$file = $key2 = $this->cleanKey($key2);
534+
$key = $partial[0];
535+
if (isset($partial[2])) {
536+
$key2 = "{$key}-{$partial[2]}";
537+
}
538+
if (($pos = strpos($key2, "|")) !== false) {
539+
$key2 = substr($key2, 0, $pos);
540+
}
541+
}
542+
return [$key, $key2];
543+
}
544+
545+
/**
546+
* Clean key
547+
* @param string $key
548+
* @return string
549+
*/
550+
final protected function cleanKey(string $key): string
551+
{
552+
if(strpos($key, "!") === 0) {
553+
return substr($key, 1);
554+
}
555+
return $key;
556+
}
526557
}

0 commit comments

Comments
 (0)