Skip to content

Commit

Permalink
fixes #58 and #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Schwarz authored and usox committed Aug 10, 2018
1 parent 092bb6e commit 4c4fc9c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 62 deletions.
112 changes: 58 additions & 54 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -94,7 +98,7 @@ public function __clone()
*/
public function setParent(Context $parent): void
{
$this->parentContext = $parent;
$this->_parentContext = $parent;
}

/**
Expand All @@ -107,7 +111,7 @@ public function setParent(Context $parent): void
*/
public function setGlobal(\stdClass $globalContext): void
{
$this->globalContext = $globalContext;
$this->_globalContext = $globalContext;
}

/**
Expand All @@ -129,15 +133,15 @@ public function pushContext(): Context
*/
public function popContext(): Context
{
return $this->parentContext;
return $this->_parentContext;
}

/**
* @param bool $tf true if DOCTYPE and XML declaration should be echoed immediately, false if buffered
*/
public function echoDeclarations(bool $tf): void
{
$this->echoDeclarations = $tf;
$this->_echoDeclarations = $tf;
}

/**
Expand All @@ -156,22 +160,22 @@ 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. ' .
'Remove DOCTYPE or use PHPTAL->execute().'
);
}
echo $doctype;
} elseif (!$this->docType) {
$this->docType = $doctype;
} elseif (!$this->_docType) {
$this->_docType = $doctype;
}
}

Expand All @@ -191,22 +195,22 @@ 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.' .
' Remove XML declaration or use PHPTAL->execute().'
);
}
echo $xmldec . "\n";
} elseif (!$this->xmlDeclaration) {
$this->xmlDeclaration = $xmldec;
} elseif (!$this->_xmlDeclaration) {
$this->_xmlDeclaration = $xmldec;
}
}

Expand All @@ -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;
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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 '';
Expand All @@ -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 '';
Expand All @@ -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;
}
}

Expand All @@ -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];
}
}

Expand All @@ -330,8 +334,8 @@ public function fillSlotCallback(
*/
public function pushSlots(): void
{
$this->slotsStack[] = $this->slots;
$this->slots = [];
$this->_slotsStack[] = $this->_slots;
$this->_slots = [];
}

/**
Expand All @@ -341,7 +345,7 @@ public function pushSlots(): void
*/
public function popSlots(): void
{
$this->slots = array_pop($this->slotsStack);
$this->_slots = array_pop($this->_slotsStack);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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");
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/PHPTAL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4c4fc9c

Please sign in to comment.