Skip to content

Commit

Permalink
Fix compatible AssetVariable::offsetGet() between different PHP ver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
yurabakhtin authored and luke- committed Mar 28, 2023
1 parent b5f45ae commit 304fc28
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions modules/template/models/AssetVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*
* A OwnerContent instance is used to assign a TemplateElement to a specific
* Content of a specific type.
*
* @warning Compatible only with PHP8.0+, Don't use for PHP versions <= 7.4!
*/
class AssetVariable implements \ArrayAccess
{
Expand Down
62 changes: 62 additions & 0 deletions modules/template/models/AssetVariablePhp74.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace humhub\modules\custom_pages\modules\template\models;

use Yii;

/**
* This is the model class for table "custom_pages_template_content".
*
* A OwnerContent instance is used to assign a TemplateElement to a specific
* Content of a specific type.
*
* @deprecated Use this only for PHP7.4 and older versions
*/
class AssetVariablePhp74 implements \ArrayAccess
{

private $module;

public function get($name)
{
$path = '/'.$name;

$module = $this->getModule();
if($module->isPublished($path)) {
return $this->getModule()->getPublishedUrl($path);
}
return '';
}

private function getModule()
{
if($this->module == null) {
$this->module = Yii::$app->getModule('custom_pages');
}
return $this->module;
}

public function __toString()
{
return '';
}

public function offsetExists($offset): bool
{
return true;
}

public function offsetGet($offset)
{
return $this->get($offset);
}

public function offsetSet($offset, $value): void
{
}

public function offsetUnset($offset): void
{
}

}
2 changes: 1 addition & 1 deletion modules/template/models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function render(ActiveRecord $owner = null, $editMode = false, $container
$content[$contentElement->element_name] = new OwnerContentVariable(['ownerContent' => $contentElement, 'options' => $options]);
}

$content['assets'] = new AssetVariable();
$content['assets'] = PHP_VERSION_ID >= 80000 ? new AssetVariable() : new AssetVariablePhp74();

if($containerItem) {
//$content['item'] = new ContainerItemVariable(['item' => $containerItem]);
Expand Down

0 comments on commit 304fc28

Please sign in to comment.