Initial move of configuration to its own class and factory. #951

This commit is contained in:
2022-08-21 19:21:03 +02:00
parent e7500b76eb
commit f8ac247377
17 changed files with 885 additions and 466 deletions

View File

@ -0,0 +1,178 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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;
use Joomla\Registry\Registry;
use VDM\Joomla\Utilities\Component\Helper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Utilities\StringHelper;
/**
* Compiler Configurations Registry
*
* @since 3.1.6
*/
class Config extends Registry implements \JsonSerializable, \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* The Params
*
* @var Registry
* @since 3.1.6
*/
protected Registry $params;
/**
* Constructor
*
* @param array $config The data to bind to the new Config object.
* @param Registry $params The component parameters
*
* @since 3.1.6
*/
public function __construct(array $config, ?Registry $params = null)
{
// Set the params
$this->params = $params ?: Helper::getParams('com_componentbuilder');
// Instantiate the internal data object.
$this->data = new \stdClass;
// Load the config to the data object
$this->bindData($this->data, $this->modelConfig($config));
}
/**
* model the configuration data array
*
* @param array $config The data to bind to the new Config object.
*
* @return array
* @since 3.1.6
*/
protected function modelConfig(array $config): array
{
// we do not yet have this set as an option
$config['remove_line_breaks']
= 2; // 2 is global (use the components value)
// set the minfy switch of the JavaScript
$config['minify'] = (isset($config['minify']) && $config['minify'] != 2)
? $config['minify'] : $this->params->get('minify', 0);
// set the global language
$config['lang_tag'] = $this->params->get('language', 'en-GB');
// check if we have Tidy enabled
$config['tidy'] = extension_loaded('Tidy');
// set the field type builder
$config['field_builder_type'] = $this->params->get(
'compiler_field_builder_type', 2
);
// load the compiler path
$config['compiler_path'] = $this->params->get(
'compiler_folder_path',
JPATH_COMPONENT_ADMINISTRATOR . '/compiler'
);
// load the jcb powers path
$config['jcb_powers_path'] = $this->params->get(
'jcb_powers_path',
'libraries/jcb_powers');
// set the component ID
$config['component_id'] = (int)$config['component'];
// TODO set up stream correctly
unset($config['component']);
// set this components code name
if ($name_code = GetHelper::var(
'joomla_component', $config['component_id'], 'id', 'name_code'
)) {
// set lang prefix
$config['lang_prefix'] = 'COM_' . StringHelper::safe(
$name_code, 'U'
);
// set component code name
$config['component_code_name'] = StringHelper::safe(
$name_code
);
// set component context
$config['component_context'] = $config['component_code_name'] . '.'
. $config['component_id'];
// set the component name length
$config['component_code_name_length'] = strlen(
$config['component_code_name']
);
// add assets table fix
$global = (int)$this->params->get(
'assets_table_fix', 1
);
$config['add_assets_table_fix'] = (($add_assets_table_fix
= (int)GetHelper::var(
'joomla_component', $config['component_id'], 'id',
'assets_table_fix'
)) == 3) ? $global : $add_assets_table_fix;
// set if language strings line breaks should be removed
$global = ((int)GetHelper::var(
'joomla_component', $config['component_id'], 'id',
'remove_line_breaks'
) == 1) ? true : false;
$config['remove_line_breaks'] = ((int)$config['remove_line_breaks']
== 0)
? false
: (((int)$config['remove_line_breaks'] == 1) ? true
: $global);
// set if placeholders should be added to customcode
$global = ((int)GetHelper::var(
'joomla_component', $config['component_id'], 'id',
'add_placeholders'
) == 1) ? true : false;
$config['add_placeholders'] = ((int)$config['placeholders'] == 0)
? false
: (((int)$config['placeholders'] == 1) ? true : $global);
// TODO set up stream correctly
unset($config['placeholders']);
// set if line numbers should be added to comments
$global = ((int)GetHelper::var(
'joomla_component', $config['component_id'], 'id',
'debug_linenr'
) == 1) ? true : false;
$config['debug_line_nr'] = ((int)$config['debuglinenr'] == 0) ? false
: (((int)$config['debuglinenr'] == 1) ? true : $global);
// set if powers should be added to component (default is true)
$global = ((int)GetHelper::var(
'joomla_component', $config['component_id'], 'id',
'add_powers'
) == 1) ? true : false;
$config['add_power'] = (isset($config['powers']) && (int)$config['powers'] == 0)
? false : ((isset($config['powers']) && (int)$config['powers'] == 1) ? true : $global);
// TODO set up stream correctly
unset($config['powers']);
}
return $config;
}
}

View File

@ -131,14 +131,8 @@ class InstallScript
* Constructor
* @since 3.1.5
*/
public function __construct(object $extension, ?array $config = null)
public function __construct(object $extension)
{
// check if we have the debug switch TODO - move to global registry - singleton
if (isset($config['debug']))
{
$this->debug = $config['debug'];
}
// loop over methods and types
foreach ($this->methods as $method)
{

View File

@ -0,0 +1,257 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @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\Factory\Compiler;
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;
use VDM\Joomla\Componentbuilder\Compiler\Config as CompilerConfig;
use VDM\Joomla\Utilities\ArrayHelper;
/**
* Factory to load the compiler config
*/
abstract class Config
{
/**
* Global Config object
*
* @var CompilerConfig
* @since 3.1.6
**/
protected static $CompilerConfig = null;
/**
* Get a value.
*
* @param string $path Registry path (e.g. version)
* @param mixed $default Optional default value, returned if the internal value is null.
*
* @return mixed Value of entry or null
*
* @since 3.1.6
*/
public static function get(string $path, $default = null)
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
// return the value or default if none is found
return self::$CompilerConfig->get($path, $default);
}
/**
* Check if a registry path exists.
*
* @param string $path Registry path (e.g. guid.main.0.path)
*
* @return boolean
*
* @since 3.1.6
*/
public static function exists($path)
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
// check if exists
return self::$CompilerConfig->exists($path);
}
/**
* Method to extract a sub-registry from path
*
* @param string $path Registry path (e.g. guid.main)
*
* @return Registry Registry object (empty if no data is present)
*
* @since 3.1.6
*/
public function extract($path)
{
if (!self::exists($path))
{
// create config registry
return new Registry();
}
return self::$CompilerConfig->extract($path);
}
/**
* Gets this object represented as an ArrayIterator.
*
* This allows the data properties to be accessed via a foreach statement.
*
* @return \ArrayIterator This object represented as an ArrayIterator.
*
* @see IteratorAggregate::getIterator()
* @since 3.1.6
*/
#[\ReturnTypeWillChange]
public static function getIterator()
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
return self::$CompilerConfig->getIterator();
}
/**
* Set a registry value.
*
* @param string $path Registry Path (e.g. guid.main.0.url)
* @param mixed $value Value of entry
* @param string $separator The key separator
*
* @return mixed The value of the that has been set.
*
* @since 3.1.6
*/
public static function set($path, $value, $separator = null)
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
self::$CompilerConfig->set($path, $value, $separator);
}
/**
* Delete a registry value
*
* @param string $path Registry Path (e.g. guid.main.0.url)
*
* @return mixed The value of the removed node or null if not set
*
* @since 3.1.6
*/
public static function remove($path)
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
// remove the actual value
return self::$CompilerConfig->remove($path);
}
/**
* Transforms a namespace to an array
*
* @return array An associative array holding the namespace data
*
* @since 3.1.6
*/
public static function toArray()
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
return self::$CompilerConfig->toArray();
}
/**
* Transforms a namespace to an object
*
* @return object An an object holding the namespace data
*
* @since 3.1.6
*/
public static function toObject()
{
// check that if we already have config registry set
if (!self::$CompilerConfig)
{
// create config registry
self::$CompilerConfig = self::create();
}
return self::$CompilerConfig->toObject();
}
/**
* Initialize a CompilerConfig object if id does not exist.
*
* Returns the global {@link CompilerConfig} object, only creating it if it doesn't already exist.
*
* @param array $config The data to bind to the new Config object.
*
*
* @return CompilerConfig object
*
* @see Session
* @since 3.1.6
**/
public static function init($config = null): CompilerConfig
{
if (!self::$CompilerConfig)
{
self::$CompilerConfig = self::create($config);
}
return self::$CompilerConfig;
}
/**
* Create a CompilerConfig object
*
* @param array $config The data to bind to the new Config object.
*
* @return CompilerConfig object
* @since 3.1.6
* @throws \Exception
**/
protected static function create($config = null): CompilerConfig
{
// get the session
$session = Factory::getSession();
// check if we have config
if (ArrayHelper::check($config))
{
// save for later should we call this out of scope
$session->set('Componentbuilder.Compiler.Config', $config);
}
// if not found try loading it from the session
elseif (($config = $session->get('Componentbuilder.Compiler.Config', false)) === false)
{
throw new \Exception('Compiler configuration not found.');
}
return new CompilerConfig($config);
}
}

View File

@ -12,6 +12,9 @@
namespace VDM\Joomla\Componentbuilder;
use VDM\Joomla\Componentbuilder\Factory\Compiler\Config;
/**
* Add line comment
*
@ -19,14 +22,6 @@ namespace VDM\Joomla\Componentbuilder;
*/
trait Line
{
/**
* The debug switch
*
* @var bool
* @since 3.1.5
*/
protected bool $debug = false;
/**
* Set the line number in comments
*
@ -37,7 +32,7 @@ trait Line
*/
private function setLine(int $nr): string
{
if ($this->debug)
if (Config::get('debug_line_nr', false))
{
return ' [' . get_called_class() . ' ' . $nr . ']';
}