Moved all placeholder behaviour to class function. Moved dynamic content to the content class.
This commit is contained in:
@ -416,6 +416,28 @@ class Config extends BaseConfig
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should we remove the site folder
|
||||
*
|
||||
* @return bool Switch to control the removal
|
||||
* @since 3.2.0
|
||||
*/
|
||||
protected function getRemovesitefolder(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should we remove the site edit folder
|
||||
*
|
||||
* @return bool Switch to control the removal
|
||||
* @since 3.2.0
|
||||
*/
|
||||
protected function getRemovesiteeditfolder(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -104,6 +104,19 @@ class Content
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove(string $key)
|
||||
{
|
||||
unset($this->active[Placefix::_h($key)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set dynamic content
|
||||
*
|
||||
@ -122,15 +135,15 @@ class Content
|
||||
/**
|
||||
* Get dynamic content
|
||||
*
|
||||
* @param string $view The view key
|
||||
* @param string $key The main string key
|
||||
* @param string $view The view key
|
||||
* @param string|null $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get_(string $view, string $key = null)
|
||||
public function get_(string $view, ?string $key = null)
|
||||
{
|
||||
if (!is_null($key))
|
||||
if (is_string($key))
|
||||
{
|
||||
return $this->_active[$view][Placefix::_h($key)] ?? null;
|
||||
}
|
||||
@ -140,19 +153,23 @@ class Content
|
||||
/**
|
||||
* Does view key exist
|
||||
*
|
||||
* @param string $view The view key
|
||||
* @param string $key The main string key
|
||||
* @param string $view The view key
|
||||
* @param string|null $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist_(string $view, string $key): bool
|
||||
public function exist_(string $view, ?string $key = null): bool
|
||||
{
|
||||
if (isset($this->_active[$view]) &&
|
||||
if (is_string($key) && isset($this->_active[$view]) &&
|
||||
isset($this->_active[$view][Placefix::_h($key)]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
elseif (is_null($key) && isset($this->_active[$view]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -178,6 +195,27 @@ class Content
|
||||
$this->_active[$view][Placefix::_h($key)] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove dynamic content
|
||||
*
|
||||
* @param string $view The view key
|
||||
* @param string|null $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove_(string $view, ?string $key = null)
|
||||
{
|
||||
if (is_string($key))
|
||||
{
|
||||
unset($this->_active[$view][Placefix::_h($key)]);
|
||||
}
|
||||
else
|
||||
{
|
||||
unset($this->_active[$view]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,164 @@ namespace VDM\Joomla\Componentbuilder\Compiler\Interfaces;
|
||||
*/
|
||||
interface PlaceholderInterface
|
||||
{
|
||||
/**
|
||||
* Set content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
* @param bool $hash Add the hash around the key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set(string $key, $value, bool $hash = true);
|
||||
|
||||
/**
|
||||
* Get content by key
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get(string $key);
|
||||
|
||||
/**
|
||||
* Does key exist at all in any variation
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist(string $key): bool;
|
||||
|
||||
/**
|
||||
* Add content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
* @param bool $hash Add the hash around the key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add(string $key, $value, bool $hash = true);
|
||||
|
||||
/**
|
||||
* Remove content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove(string $key);
|
||||
|
||||
/**
|
||||
* Set content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set_(string $key, $value);
|
||||
|
||||
/**
|
||||
* Get content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get_(string $key);
|
||||
|
||||
/**
|
||||
* Does key exist with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist_(string $key): bool;
|
||||
|
||||
/**
|
||||
* Add content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add_(string $key, $value);
|
||||
|
||||
/**
|
||||
* Remove content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove_(string $key);
|
||||
|
||||
/**
|
||||
* Set content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set_h(string $key, $value);
|
||||
|
||||
/**
|
||||
* Get content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get_h(string $key);
|
||||
|
||||
/**
|
||||
* Does key exist with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist_h(string $key): bool;
|
||||
|
||||
/**
|
||||
* Add content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add_h(string $key, $value);
|
||||
|
||||
/**
|
||||
* Remove content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove_h(string $key);
|
||||
|
||||
/**
|
||||
* Set a type of placeholder with set of values
|
||||
*
|
||||
@ -57,6 +215,16 @@ interface PlaceholderInterface
|
||||
*/
|
||||
public function update(string $data, array &$placeholder, int $action = 1): string;
|
||||
|
||||
/**
|
||||
* Update the data with the active placeholders
|
||||
*
|
||||
* @param string $data The actual data
|
||||
*
|
||||
* @return string
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function update_(string $data): string;
|
||||
|
||||
/**
|
||||
* return the placeholders for inserted and replaced code
|
||||
*
|
||||
|
@ -13,8 +13,6 @@ namespace VDM\Joomla\Componentbuilder\Compiler;
|
||||
|
||||
|
||||
use VDM\Joomla\Utilities\ArrayHelper;
|
||||
use VDM\Joomla\Utilities\StringHelper;
|
||||
use VDM\Joomla\Utilities\GetHelper;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Config;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Utilities\Placefix;
|
||||
@ -56,6 +54,270 @@ class Placeholder implements PlaceholderInterface
|
||||
$this->config = $config ?: Compiler::_('Config');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
* @param bool $hash Add the hash around the key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set(string $key, $value, bool $hash = true)
|
||||
{
|
||||
if ($hash)
|
||||
{
|
||||
$this->set_($key, $value);
|
||||
$this->set_h($key, $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->active[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content by key
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get(string $key)
|
||||
{
|
||||
return $this->active[$key] ?? $this->get_($key) ?? $this->get_h($key) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does key exist at all in any variation
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist(string $key): bool
|
||||
{
|
||||
if (isset($this->active[$key]) || $this->exist_($key) || $this->exist_h($key))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
* @param bool $hash Add the hash around the key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add(string $key, $value, bool $hash = true)
|
||||
{
|
||||
if ($hash)
|
||||
{
|
||||
$this->add_($key, $value);
|
||||
$this->add_h($key, $value);
|
||||
}
|
||||
elseif (isset($this->active[$key]))
|
||||
{
|
||||
$this->active[$key] .= $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->active[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove content
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove(string $key)
|
||||
{
|
||||
if (isset($this->active[$key]))
|
||||
{
|
||||
unset($this->active[$key]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->remove_($key);
|
||||
$this->remove_h($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set_(string $key, $value)
|
||||
{
|
||||
$this->active[Placefix::_($key)] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get_(string $key)
|
||||
{
|
||||
return $this->active[Placefix::_($key)] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does key exist with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist_(string $key): bool
|
||||
{
|
||||
if (isset($this->active[Placefix::_($key)]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add_(string $key, $value)
|
||||
{
|
||||
if (isset($this->active[Placefix::_($key)]))
|
||||
{
|
||||
$this->active[Placefix::_($key)] .= $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->active[Placefix::_($key)] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove content with [ [ [ ... ] ] ] hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove_(string $key)
|
||||
{
|
||||
if ($this->exist_($key))
|
||||
{
|
||||
unset($this->active[Placefix::_($key)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set_h(string $key, $value)
|
||||
{
|
||||
$this->active[Placefix::_h($key)] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return mixed
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get_h(string $key)
|
||||
{
|
||||
return $this->active[Placefix::_h($key)] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does key exist with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function exist_h(string $key): bool
|
||||
{
|
||||
if (isset($this->active[Placefix::_h($key)]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
* @param mixed $value The values to set
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function add_h(string $key, $value)
|
||||
{
|
||||
if ($this->exist_h($key))
|
||||
{
|
||||
$this->active[Placefix::_h($key)] .= $value;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->active[Placefix::_h($key)] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove content with # # # hash
|
||||
*
|
||||
* @param string $key The main string key
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function remove_h(string $key)
|
||||
{
|
||||
if ($this->exist_h($key))
|
||||
{
|
||||
unset($this->active[Placefix::_h($key)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a type of placeholder with set of values
|
||||
*
|
||||
@ -76,8 +338,7 @@ class Placeholder implements PlaceholderInterface
|
||||
$number = 0;
|
||||
foreach ($values as $value)
|
||||
{
|
||||
$this->active[Placefix::_($key . $number)]
|
||||
= $value;
|
||||
$this->set($key . $number, $value);
|
||||
$number++;
|
||||
}
|
||||
}
|
||||
@ -93,15 +354,18 @@ class Placeholder implements PlaceholderInterface
|
||||
*/
|
||||
public function clearType(string $key)
|
||||
{
|
||||
$key = Placefix::_($key);
|
||||
$keys = [Placefix::_($key), Placefix::_h($key), $key];
|
||||
|
||||
$this->active = array_filter(
|
||||
$this->active,
|
||||
function(string $k) use($key){
|
||||
return preg_replace('/\d/', '', $k) !== $key;
|
||||
},
|
||||
ARRAY_FILTER_USE_KEY
|
||||
);
|
||||
foreach ($keys as $_key)
|
||||
{
|
||||
$this->active = array_filter(
|
||||
$this->active,
|
||||
function (string $k) use ($_key) {
|
||||
return preg_replace('/\d/', '', $k) !== $_key;
|
||||
},
|
||||
ARRAY_FILTER_USE_KEY
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -124,9 +388,9 @@ class Placeholder implements PlaceholderInterface
|
||||
// make sure the placeholders is an array
|
||||
if (!ArrayHelper::check($placeholder))
|
||||
{
|
||||
// This is an error, TODO actually we need to add a kind of log here to know that this happened
|
||||
return $data;
|
||||
}
|
||||
|
||||
// continue with the work of replacement
|
||||
if (1 == $action) // <-- just replace (default)
|
||||
{
|
||||
@ -148,7 +412,6 @@ class Placeholder implements PlaceholderInterface
|
||||
// only replace if the data has these placeholder values
|
||||
if ($replace === true)
|
||||
{
|
||||
|
||||
return str_replace(
|
||||
array_keys($placeholder), array_values($placeholder), $data
|
||||
);
|
||||
@ -176,6 +439,22 @@ class Placeholder implements PlaceholderInterface
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the data with the active placeholders
|
||||
*
|
||||
* @param string $data The actual data
|
||||
*
|
||||
* @return string
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function update_(string $data): string
|
||||
{
|
||||
// just replace the placeholders in data
|
||||
return str_replace(
|
||||
array_keys($this->active), array_values($this->active), $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the placeholders for inserted and replaced code
|
||||
*
|
||||
@ -239,7 +518,6 @@ class Placeholder implements PlaceholderInterface
|
||||
|
||||
return [ 'start' => "", 'end' => ""];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,22 @@ class Power implements PowerInterface
|
||||
**/
|
||||
public array $active = [];
|
||||
|
||||
/**
|
||||
* All power namespaces
|
||||
*
|
||||
* @var array
|
||||
* @since 3.2.0
|
||||
**/
|
||||
public array $namespace = [];
|
||||
|
||||
/**
|
||||
* All composer namespaces
|
||||
*
|
||||
* @var array
|
||||
* @since 3.2.0
|
||||
**/
|
||||
public array $composer = [];
|
||||
|
||||
/**
|
||||
* The url to the power, if there is an error.
|
||||
*
|
||||
@ -475,10 +491,13 @@ class Power implements PowerInterface
|
||||
$namespace_array = array_map(function ($val) {
|
||||
return $this->getCleanNamespace($val);
|
||||
}, $namespace_array);
|
||||
|
||||
// set the actual class namespace
|
||||
$this->active[$guid]->_namespace = implode('\\', $namespace_array);
|
||||
// prefix values
|
||||
$this->active[$guid]->_namespace_prefix = $path_array;
|
||||
|
||||
// set global namespaces for autoloader
|
||||
$this->namespace[implode('.', $path_array)] = $path_array;
|
||||
|
||||
// get the parent folder (the first value in array)
|
||||
$prefix_folder = implode('.', $path_array);
|
||||
|
||||
@ -579,10 +598,10 @@ class Power implements PowerInterface
|
||||
$this->active[$guid]->composer
|
||||
)) ? json_decode($this->active[$guid]->composer, true) : null;
|
||||
|
||||
unset($this->active[$guid]->composer);
|
||||
|
||||
if (ArrayHelper::check($_composer))
|
||||
{
|
||||
$this->active[$guid]->composer = [];
|
||||
|
||||
foreach ($_composer as $composer)
|
||||
{
|
||||
if (isset($composer['access_point']) && StringHelper::check($composer['access_point']))
|
||||
@ -612,17 +631,15 @@ class Power implements PowerInterface
|
||||
{
|
||||
$namespace = $this->getCleanNamespace(explode(' as ', $namespace)[0]);
|
||||
}
|
||||
$this->active[$guid]->composer[$namespace] = $composer['access_point'];
|
||||
|
||||
// add composer namespaces for autoloader
|
||||
$this->composer[$namespace] = $composer['access_point'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->active[$guid]->composer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Component.Builder
|
||||
*
|
||||
* @created 4th September, 2022
|
||||
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
||||
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
||||
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace VDM\Joomla\Componentbuilder\Compiler\Power;
|
||||
|
||||
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Power;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Config;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Content;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Utilities\Line;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Utilities\Indent;
|
||||
use VDM\Joomla\Utilities\ArrayHelper;
|
||||
|
||||
|
||||
/**
|
||||
* Compiler Autoloader
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
class Autoloader
|
||||
{
|
||||
/**
|
||||
* Power Objects
|
||||
*
|
||||
* @var Power
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Power $power;
|
||||
|
||||
/**
|
||||
* Compiler Config
|
||||
*
|
||||
* @var Config
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Config $config;
|
||||
|
||||
/**
|
||||
* Compiler Content
|
||||
*
|
||||
* @var Content
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Content $content;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Power|null $power The power object.
|
||||
* @param Config|null $config The compiler config object.
|
||||
* @param Content|null $content The compiler content object.
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function __construct(?Power $power = null, ?Config $config = null, ?Content $content = null)
|
||||
{
|
||||
$this->power = $power ?: Compiler::_('Power');
|
||||
$this->config = $config ?: Compiler::_('Config');
|
||||
$this->content = $content ?: Compiler::_('Content');
|
||||
|
||||
// reset all autoloaders power placeholders
|
||||
$this->content->set('ADMIN_POWER_HELPER', '');
|
||||
$this->content->set('SITE_POWER_HELPER', '');
|
||||
$this->content->set('CUSTOM_POWER_AUTOLOADER', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the autoloader into the active content array
|
||||
*
|
||||
* @return void
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set()
|
||||
{
|
||||
if (($size = ArrayHelper::check($this->power->namespace)) > 0)
|
||||
{
|
||||
// set if we should load the autoloader on the site area
|
||||
$loadSite = true;
|
||||
// check if we are using a plugin
|
||||
$use_plugin = $this->content->exist('PLUGIN_POWER_AUTOLOADER');
|
||||
// build the methods
|
||||
$autoloadNotSiteMethod = array();
|
||||
$autoloadMethod = array();
|
||||
// add only if we are not using a plugin
|
||||
$tab_space = 2;
|
||||
if (!$use_plugin)
|
||||
{
|
||||
$autoloadNotSiteMethod[] = PHP_EOL . PHP_EOL;
|
||||
$tab_space = 0;
|
||||
}
|
||||
elseif (!$loadSite)
|
||||
{
|
||||
// we add code to prevent this plugin from triggering on the site area
|
||||
$autoloadNotSiteMethod[] = PHP_EOL . Indent::_(2) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' do not run the autoloader in the site area';
|
||||
$autoloadNotSiteMethod[] = Indent::_(2) . 'if ($this->app->isClient(\'site\'))';
|
||||
$autoloadNotSiteMethod[] = Indent::_(2) . '{';
|
||||
$autoloadNotSiteMethod[] = Indent::_(3) . 'return;';
|
||||
$autoloadNotSiteMethod[] = Indent::_(2) . '}' . PHP_EOL;
|
||||
}
|
||||
// we start building the spl_autoload_register function call
|
||||
$autoloadMethod[] = Indent::_($tab_space) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' register this component namespace';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . 'spl_autoload_register(function ($class) {';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' project-specific base directories and namespace prefix';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$search = array(';
|
||||
// ==== IMPORTANT NOTICE =====
|
||||
// make sure the name space values are sorted from the longest string to the shortest
|
||||
// so that the search do not mistakenly match a shorter namespace before a longer one
|
||||
// that has the same short namespace for example:
|
||||
// NameSpace\SubName\Sub <- will always match first
|
||||
// NameSpace\SubName\SubSubName
|
||||
// Should the shorter namespace be listed [first] it will match both of these:
|
||||
// NameSpace\SubName\Sub\ClassName
|
||||
// ^^^^^^^^^^^^^^^^^^^^^
|
||||
// NameSpace\SubName\SubSubName\ClassName
|
||||
// ^^^^^^^^^^^^^^^^^^^^^
|
||||
uksort($this->power->namespace, function ($a, $b) {
|
||||
return strlen($b) - strlen($a);
|
||||
});
|
||||
// counter to manage the comma in the actual array
|
||||
$counter = 1;
|
||||
foreach ($this->power->namespace as $base_dir => $prefix)
|
||||
{
|
||||
// don't add the ending comma on last value
|
||||
if ($size == $counter)
|
||||
{
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . "'" . $this->config->get('jcb_powers_path', 'libraries/jcb_powers') . "/$base_dir' => '" . implode('\\\\', $prefix) . "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . "'" . $this->config->get('jcb_powers_path', 'libraries/jcb_powers') . "/$base_dir' => '" . implode('\\\\', $prefix) . "',";
|
||||
}
|
||||
$counter++;
|
||||
}
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . ');';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '// Start the search and load if found';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$found = false;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$found_base_dir = "";';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$found_len = 0;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . 'foreach ($search as $base_dir => $prefix)';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '{';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' does the class use the namespace prefix?';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . '$len = strlen($prefix);';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . 'if (strncmp($prefix, $class, $len) === 0)';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . '{';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' we have a match so load the values';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . '$found = true;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . '$found_base_dir = $base_dir;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . '$found_len = $len;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' done here';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(3) . 'break;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . '}';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '}';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' check if we found a match';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . 'if (!$found)';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '{';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' no, move to the next registered autoloader';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . 'return;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '}';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' get the relative class name';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$relative_class = substr($class, $found_len);';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' replace the namespace prefix with the base directory, replace namespace';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '// separators with directory separators in the relative class name, append';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '// with .php';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . "\$file = JPATH_ROOT . '/' . \$found_base_dir . '/src' . str_replace('\\\\', '/', \$relative_class) . '.php';";
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//'
|
||||
. Line::_(__Line__, __Class__) . ' if the file exists, require it';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . 'if (file_exists($file))';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '{';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . 'require $file;';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '}';
|
||||
$autoloadMethod[] = Indent::_($tab_space) . '});';
|
||||
// create the method string
|
||||
$autoloader = implode(PHP_EOL, $autoloadNotSiteMethod) . implode(PHP_EOL, $autoloadMethod);
|
||||
// check if we are using a plugin
|
||||
if ($use_plugin)
|
||||
{
|
||||
$this->content->set('PLUGIN_POWER_AUTOLOADER', PHP_EOL . $autoloader);
|
||||
}
|
||||
else
|
||||
{
|
||||
// load to events placeholders
|
||||
$this->content->add('ADMIN_POWER_HELPER', $autoloader);
|
||||
// load to site if needed
|
||||
if ($loadSite)
|
||||
{
|
||||
$this->content->add('SITE_POWER_HELPER', $autoloader);
|
||||
}
|
||||
}
|
||||
// to add to custom files
|
||||
$this->content->add('CUSTOM_POWER_AUTOLOADER', PHP_EOL . implode(PHP_EOL, $autoloadMethod));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
<html><body bgcolor="#FFFFFF"></body></html>
|
@ -14,7 +14,8 @@ namespace VDM\Joomla\Componentbuilder\Compiler\Service;
|
||||
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Power as CompilerPower;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Power as Powers;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Power\Autoloader;
|
||||
|
||||
|
||||
/**
|
||||
@ -34,26 +35,47 @@ class Power implements ServiceProviderInterface
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->alias(CompilerPower::class, 'Power')
|
||||
->share('Power', [$this, 'getPower'], true);
|
||||
$container->alias(Powers::class, 'Power')
|
||||
->share('Power', [$this, 'getPowers'], true);
|
||||
|
||||
$container->alias(Autoloader::class, 'Power.Autoloader')
|
||||
->share('Power.Autoloader', [$this, 'getAutoloader'], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Compiler Power
|
||||
* Get the Powers
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return CompilerPower
|
||||
* @return Powers
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function getPower(Container $container): CompilerPower
|
||||
public function getPowers(Container $container): Powers
|
||||
{
|
||||
return new CompilerPower(
|
||||
return new Powers(
|
||||
$container->get('Config'),
|
||||
$container->get('Placeholder'),
|
||||
$container->get('Customcode'),
|
||||
$container->get('Customcode.Gui')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Compiler Autoloader
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return Autoloader
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function getAutoloader(Container $container): Autoloader
|
||||
{
|
||||
return new Autoloader(
|
||||
$container->get('Power'),
|
||||
$container->get('Config'),
|
||||
$container->get('Content')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user