diff --git a/src/Context.php b/src/Context.php index cde14a44..87303bd4 100644 --- a/src/Context.php +++ b/src/Context.php @@ -28,46 +28,50 @@ class Context */ public $repeat; + /** + * n.b. The following variables *must* be prefixed with an underscore '_' for reasons... + */ + /** * @var string */ - public $xmlDeclaration; + public $_xmlDeclaration; /** * @var string */ - public $docType; + public $_docType; /** * @var bool */ - private $nothrow; + private $_nothrow; /** * @var array TODO What type? */ - private $slots = []; + private $_slots = []; /** * @var array */ - private $slotsStack = []; + private $_slotsStack = []; /** * @var Context */ - private $parentContext; + private $_parentContext; /** * @var \stdClass */ - private $globalContext; + private $_globalContext; /** * @var bool */ - private $echoDeclarations = false; + private $_echoDeclarations = false; /** * Context constructor. @@ -94,7 +98,7 @@ public function __clone() */ public function setParent(Context $parent): void { - $this->parentContext = $parent; + $this->_parentContext = $parent; } /** @@ -107,7 +111,7 @@ public function setParent(Context $parent): void */ public function setGlobal(\stdClass $globalContext): void { - $this->globalContext = $globalContext; + $this->_globalContext = $globalContext; } /** @@ -129,7 +133,7 @@ public function pushContext(): Context */ public function popContext(): Context { - return $this->parentContext; + return $this->_parentContext; } /** @@ -137,7 +141,7 @@ public function popContext(): Context */ public function echoDeclarations(bool $tf): void { - $this->echoDeclarations = $tf; + $this->_echoDeclarations = $tf; } /** @@ -156,13 +160,13 @@ public function setDocType(string $doctype, bool $called_from_macro): void { // FIXME: this is temporary workaround for problem of DOCTYPE disappearing in cloned // FIXME: PHPTAL object (because clone keeps _parentContext) - if (!$this->docType) { - $this->docType = $doctype; + if (!$this->_docType) { + $this->_docType = $doctype; } - if ($this->parentContext) { - $this->parentContext->setDocType($doctype, $called_from_macro); - } elseif ($this->echoDeclarations) { + if ($this->_parentContext) { + $this->_parentContext->setDocType($doctype, $called_from_macro); + } elseif ($this->_echoDeclarations) { if ($called_from_macro) { throw new Exception\ConfigurationException( 'Executed macro in file with DOCTYPE when using echoExecute(). This is not supported yet. ' . @@ -170,8 +174,8 @@ public function setDocType(string $doctype, bool $called_from_macro): void ); } echo $doctype; - } elseif (!$this->docType) { - $this->docType = $doctype; + } elseif (!$this->_docType) { + $this->_docType = $doctype; } } @@ -191,13 +195,13 @@ public function setDocType(string $doctype, bool $called_from_macro): void public function setXmlDeclaration(string $xmldec, bool $called_from_macro): void { // FIXME - if (!$this->xmlDeclaration) { - $this->xmlDeclaration = $xmldec; + if (!$this->_xmlDeclaration) { + $this->_xmlDeclaration = $xmldec; } - if ($this->parentContext) { - $this->parentContext->setXmlDeclaration($xmldec, $called_from_macro); - } elseif ($this->echoDeclarations) { + if ($this->_parentContext) { + $this->_parentContext->setXmlDeclaration($xmldec, $called_from_macro); + } elseif ($this->_echoDeclarations) { if ($called_from_macro) { throw new Exception\ConfigurationException( 'Executed macro in file with XML declaration when using echoExecute(). This is not supported yet.' . @@ -205,8 +209,8 @@ public function setXmlDeclaration(string $xmldec, bool $called_from_macro): void ); } echo $xmldec . "\n"; - } elseif (!$this->xmlDeclaration) { - $this->xmlDeclaration = $xmldec; + } elseif (!$this->_xmlDeclaration) { + $this->_xmlDeclaration = $xmldec; } } @@ -220,7 +224,7 @@ public function setXmlDeclaration(string $xmldec, bool $called_from_macro): void */ public function noThrow(bool $bool): void { - $this->nothrow = $bool; + $this->_nothrow = $bool; } /** @@ -232,7 +236,7 @@ public function noThrow(bool $bool): void */ public function hasSlot(string $key): bool { - return isset($this->slots[$key]) || ($this->parentContext && $this->parentContext->hasSlot($key)); + return isset($this->_slots[$key]) || ($this->_parentContext && $this->_parentContext->hasSlot($key)); } /** @@ -246,17 +250,17 @@ public function hasSlot(string $key): bool */ public function getSlot(string $key): string { - if (isset($this->slots[$key])) { - if (is_string($this->slots[$key])) { - return $this->slots[$key]; + if (isset($this->_slots[$key])) { + if (is_string($this->_slots[$key])) { + return $this->_slots[$key]; } ob_start(); - call_user_func($this->slots[$key][0], $this->slots[$key][1], $this->slots[$key][2]); + call_user_func($this->_slots[$key][0], $this->_slots[$key][1], $this->_slots[$key][2]); return ob_get_clean(); } - if ($this->parentContext) { - return $this->parentContext->getSlot($key); + if ($this->_parentContext) { + return $this->_parentContext->getSlot($key); } return ''; @@ -273,14 +277,14 @@ public function getSlot(string $key): string */ public function echoSlot(string $key): string { - if (isset($this->slots[$key])) { - if (is_string($this->slots[$key])) { - echo $this->slots[$key]; + if (isset($this->_slots[$key])) { + if (is_string($this->_slots[$key])) { + echo $this->_slots[$key]; } else { - call_user_func($this->slots[$key][0], $this->slots[$key][1], $this->slots[$key][2]); + call_user_func($this->_slots[$key][0], $this->_slots[$key][1], $this->_slots[$key][2]); } - } elseif ($this->parentContext) { - return $this->parentContext->echoSlot($key); + } elseif ($this->_parentContext) { + return $this->_parentContext->echoSlot($key); } return ''; @@ -295,10 +299,10 @@ public function echoSlot(string $key): string */ public function fillSlot(string $key, string $content): void { - $this->slots[$key] = $content; - if ($this->parentContext) { + $this->_slots[$key] = $content; + if ($this->_parentContext) { // Works around bug with tal:define popping context after fillslot - $this->parentContext->slots[$key] = $content; + $this->_parentContext->_slots[$key] = $content; } } @@ -316,10 +320,10 @@ public function fillSlotCallback( PhpTalInterface $_thistpl, PhpTalInterface $tpl ): void { - $this->slots[$key] = [$callback, $_thistpl, $tpl]; - if ($this->parentContext) { + $this->_slots[$key] = [$callback, $_thistpl, $tpl]; + if ($this->_parentContext) { // Works around bug with tal:define popping context after fillslot - $this->parentContext->slots[$key] = [$callback, $_thistpl, $tpl]; + $this->_parentContext->_slots[$key] = [$callback, $_thistpl, $tpl]; } } @@ -330,8 +334,8 @@ public function fillSlotCallback( */ public function pushSlots(): void { - $this->slotsStack[] = $this->slots; - $this->slots = []; + $this->_slotsStack[] = $this->_slots; + $this->_slots = []; } /** @@ -341,7 +345,7 @@ public function pushSlots(): void */ public function popSlots(): void { - $this->slots = array_pop($this->slotsStack); + $this->_slots = array_pop($this->_slotsStack); } /** @@ -385,7 +389,7 @@ public function __set(string $varname, $value): void public function __isset(string $varname): bool { // it doesn't need to check isset($this->$varname), because PHP does that _before_ calling __isset() - return isset($this->globalContext->$varname) || defined($varname); + return isset($this->_globalContext->$varname) || defined($varname); } /** @@ -402,19 +406,19 @@ public function __get(string $varname) // PHP checks public properties first, there's no need to support them here // must use isset() to allow custom global contexts with __isset()/__get() - if (isset($this->globalContext->$varname)) { - return $this->globalContext->$varname; + if (isset($this->_globalContext->$varname)) { + return $this->_globalContext->$varname; } if (defined($varname)) { return constant($varname); } - if ($this->nothrow) { + if ($this->_nothrow) { return null; } - throw new Exception\VariableNotFoundException('Unable to find variable \'$varname\' in current scope'); + throw new Exception\VariableNotFoundException("Unable to find variable '$varname' in current scope"); } /** diff --git a/src/PHPTAL.php b/src/PHPTAL.php index 5df85902..a0af1322 100644 --- a/src/PHPTAL.php +++ b/src/PHPTAL.php @@ -237,8 +237,8 @@ public function setTemplate(?string $path): PhpTalInterface $this->codeFile = null; $this->path = $path; $this->source = null; - $this->context->docType = null; - $this->context->xmlDeclaration = null; + $this->context->_docType = null; + $this->context->_xmlDeclaration = null; return $this; } @@ -260,8 +260,8 @@ public function setSource(string $src, ?string $path = null): PhpTalInterface $this->codeFile = null; $this->source = new StringSource($src, $path); $this->path = $this->source->getRealPath(); - $this->context->docType = null; - $this->context->xmlDeclaration = null; + $this->context->_docType = null; + $this->context->_xmlDeclaration = null; return $this; } @@ -681,13 +681,13 @@ public function execute(): string } // unshift doctype - if ($this->context->docType) { - $res = $this->context->docType . $res; + if ($this->context->_docType) { + $res = $this->context->_docType . $res; } // unshift xml declaration - if ($this->context->xmlDeclaration) { - $res = $this->context->xmlDeclaration . "\n" . $res; + if ($this->context->_xmlDeclaration) { + $res = $this->context->_xmlDeclaration . "\n" . $res; } if ($this->postfilter !== null) {