Adds getExtensionInstallClass and setInstallMethodScript to its own class. #950
This commit is contained in:
@ -0,0 +1,388 @@
|
||||
<?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\Extension;
|
||||
|
||||
|
||||
use VDM\Joomla\Utilities\ArrayHelper;
|
||||
use VDM\Joomla\Utilities\StringHelper;
|
||||
use VDM\Joomla\Componentbuilder\Line;
|
||||
use VDM\Joomla\Componentbuilder\Tab;
|
||||
|
||||
|
||||
/**
|
||||
* Loading the Extension Installation Script Class
|
||||
*
|
||||
* @since 3.1.5
|
||||
*/
|
||||
class InstallScript
|
||||
{
|
||||
/**
|
||||
* The Line numbering Methods
|
||||
*
|
||||
* @since 3.1.5
|
||||
*/
|
||||
use Line;
|
||||
|
||||
/**
|
||||
* The Tab Adding Method
|
||||
*
|
||||
* @since 3.1.5
|
||||
*/
|
||||
use Tab;
|
||||
|
||||
/**
|
||||
* The extension
|
||||
*
|
||||
* @var object
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected object $extension;
|
||||
|
||||
/**
|
||||
* The methods
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $methods = ['php_script', 'php_preflight', 'php_postflight', 'php_method'];
|
||||
|
||||
/**
|
||||
* The types
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $types = ['construct', 'install', 'update', 'uninstall', 'discover_install'];
|
||||
|
||||
/**
|
||||
* The construct bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $construct = [];
|
||||
|
||||
/**
|
||||
* The install bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $install = [];
|
||||
|
||||
/**
|
||||
* The update bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $update = [];
|
||||
|
||||
/**
|
||||
* The uninstall bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $uninstall = [];
|
||||
|
||||
/**
|
||||
* The preflight switch
|
||||
*
|
||||
* @var bool
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected bool $preflightActive = false;
|
||||
|
||||
/**
|
||||
* The preflight bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $preflightBucket = ['install' => [], 'uninstall' => [], 'discover_install' => [], 'update' => []];
|
||||
|
||||
/**
|
||||
* The postflight switch
|
||||
*
|
||||
* @var bool
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected bool $postflightActive = false;
|
||||
|
||||
/**
|
||||
* The postflight bucket
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected array $postflightBucket = ['install' => [], 'uninstall' => [], 'discover_install' => [], 'update' => []];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @since 3.1.5
|
||||
*/
|
||||
public function __construct(object $extension)
|
||||
{
|
||||
// loop over methods and types
|
||||
foreach ($this->methods as $method)
|
||||
{
|
||||
foreach ($this->types as $type)
|
||||
{
|
||||
if (isset($extension->{'add_' . $method . '_' . $type})
|
||||
&& $extension->{'add_' . $method . '_' . $type} == 1
|
||||
&& StringHelper::check(
|
||||
$extension->{$method . '_' . $type}
|
||||
))
|
||||
{
|
||||
// add to the main methods
|
||||
if ('php_method' === $method || 'php_script' === $method)
|
||||
{
|
||||
$this->{$type}[] = $extension->{$method . '_' . $type};
|
||||
}
|
||||
else
|
||||
{
|
||||
// get the flight key
|
||||
$flight = str_replace('php_', '', $method);
|
||||
// load the script to our bucket
|
||||
$this->{$flight . 'Bucket'}[$type][] = $extension->{$method . '_' . $type};
|
||||
// show that the method is active
|
||||
$this->{$flight . 'Active'} = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->extension = $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* get install script
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
public function get(): string
|
||||
{
|
||||
// return the class
|
||||
return $this->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* build the install class
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected function build(): string
|
||||
{
|
||||
// start build
|
||||
$script = $this->head();
|
||||
|
||||
// load constructor if set
|
||||
$script .= $this->construct();
|
||||
|
||||
// load install method if set
|
||||
$script .= $this->main('install');
|
||||
|
||||
// load update method if set
|
||||
$script .= $this->main('update');
|
||||
|
||||
// load uninstall method if set
|
||||
$script .= $this->main('uninstall');
|
||||
|
||||
// load preflight method if set
|
||||
$script .= $this->flight('preflight');
|
||||
|
||||
// load postflight method if set
|
||||
$script .= $this->flight('postflight');
|
||||
|
||||
// close the class
|
||||
$script .= PHP_EOL . '}' . PHP_EOL;
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* get install script head
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected function head(): string
|
||||
{
|
||||
// get the extension
|
||||
$extension = $this->extension;
|
||||
// start build
|
||||
$script = PHP_EOL . '/**';
|
||||
$script .= PHP_EOL . ' * ' . $extension->official_name
|
||||
. ' script file.';
|
||||
$script .= PHP_EOL . ' *';
|
||||
$script .= PHP_EOL . ' * @package ' . $extension->class_name;
|
||||
$script .= PHP_EOL . ' */';
|
||||
$script .= PHP_EOL . 'class ' . $extension->installer_class_name;
|
||||
$script .= PHP_EOL . '{';
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* get constructor
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected function construct(): string
|
||||
{
|
||||
// return empty string if not set
|
||||
if (!ArrayHelper::check($this->construct))
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
// the __construct script
|
||||
$script = PHP_EOL . PHP_EOL . $this->_t(1) . '/**';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' * Constructor';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' *';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @param Joomla\CMS\Installer\InstallerAdapter $adapter The object responsible for running this script';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' */';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. 'public function __construct($adapter)';
|
||||
$script .= PHP_EOL . $this->_t(1) . '{';
|
||||
$script .= PHP_EOL . implode(PHP_EOL . PHP_EOL, $this->construct);
|
||||
// close the function
|
||||
$script .= PHP_EOL . $this->_t(1) . '}';
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* build main methods
|
||||
*
|
||||
* @param string $name the method being called
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected function main(string $name): string
|
||||
{
|
||||
// return empty string if not set
|
||||
if (!ArrayHelper::check($this->{$name}))
|
||||
{
|
||||
return '';
|
||||
}
|
||||
// load the install method
|
||||
$script = PHP_EOL . PHP_EOL . $this->_t(1) . '/**';
|
||||
$script .= PHP_EOL . $this->_t(1) . " * Called on $name";
|
||||
$script .= PHP_EOL . $this->_t(1) . ' *';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @param Joomla\CMS\Installer\InstallerAdapter $adapter The object responsible for running this script';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' *';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @return boolean True on success';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' */';
|
||||
$script .= PHP_EOL . $this->_t(1) . 'public function '
|
||||
. $name . '($adapter)';
|
||||
$script .= PHP_EOL . $this->_t(1) . '{';
|
||||
$script .= PHP_EOL . implode(PHP_EOL . PHP_EOL, $this->{$name});
|
||||
// return true
|
||||
if ('uninstall' !== $name)
|
||||
{
|
||||
$script .= PHP_EOL . $this->_t(2) . 'return true;';
|
||||
}
|
||||
// close the function
|
||||
$script .= PHP_EOL . $this->_t(1) . '}';
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
/**
|
||||
* build flight methods
|
||||
*
|
||||
* @param string $name the method being called
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected function flight(string $name): string
|
||||
{
|
||||
// return empty string if not set
|
||||
if (empty($this->{$name . 'Active'}))
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
// the pre/post function types
|
||||
$script = PHP_EOL . PHP_EOL . $this->_t(1) . '/**';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * Called before any type of action';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' *';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @param string $route Which action is happening (install|uninstall|discover_install|update)';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @param Joomla\CMS\Installer\InstallerAdapter $adapter The object responsible for running this script';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' *';
|
||||
$script .= PHP_EOL . $this->_t(1)
|
||||
. ' * @return boolean True on success';
|
||||
$script .= PHP_EOL . $this->_t(1) . ' */';
|
||||
$script .= PHP_EOL . $this->_t(1) . 'public function '
|
||||
. $name . '($route, $adapter)';
|
||||
$script .= PHP_EOL . $this->_t(1) . '{';
|
||||
$script .= PHP_EOL . $this->_t(2) . '//' . $this->setLine(
|
||||
__LINE__
|
||||
) . ' get application';
|
||||
$script .= PHP_EOL . $this->_t(2)
|
||||
. '$app = JFactory::getApplication();' . PHP_EOL;
|
||||
|
||||
// add the default version check (TODO) must make this dynamic
|
||||
if ('preflight' === $name)
|
||||
{
|
||||
$script .= PHP_EOL . $this->_t(2) . '//' . $this->setLine(
|
||||
__LINE__
|
||||
) . ' the default for both install and update';
|
||||
$script .= PHP_EOL . $this->_t(2)
|
||||
. '$jversion = new JVersion();';
|
||||
$script .= PHP_EOL . $this->_t(2)
|
||||
. "if (!\$jversion->isCompatible('3.8.0'))";
|
||||
$script .= PHP_EOL . $this->_t(2) . '{';
|
||||
$script .= PHP_EOL . $this->_t(3)
|
||||
. "\$app->enqueueMessage('Please upgrade to at least Joomla! 3.8.0 before continuing!', 'error');";
|
||||
$script .= PHP_EOL . $this->_t(3) . 'return false;';
|
||||
$script .= PHP_EOL . $this->_t(2) . '}' . PHP_EOL;
|
||||
}
|
||||
|
||||
// now add the scripts
|
||||
foreach ($this->{$name . 'Bucket'} as $route => $_script)
|
||||
{
|
||||
if (ArrayHelper::check($_script))
|
||||
{
|
||||
// set the if and script
|
||||
$script .= PHP_EOL . $this->_t(2) . "if ('" . $route
|
||||
. "' === \$route)";
|
||||
$script .= PHP_EOL . $this->_t(2) . '{';
|
||||
$script .= PHP_EOL . implode(
|
||||
PHP_EOL . PHP_EOL, $_script
|
||||
);
|
||||
$script .= PHP_EOL . $this->_t(2) . '}' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
// return true
|
||||
$script .= PHP_EOL . $this->_t(2) . 'return true;';
|
||||
// close the function
|
||||
$script .= PHP_EOL . $this->_t(1) . '}';
|
||||
|
||||
return $script;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
<html><body bgcolor="#FFFFFF"></body></html>
|
@ -0,0 +1,40 @@
|
||||
<?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;
|
||||
|
||||
|
||||
/**
|
||||
* Add line comment
|
||||
*
|
||||
* @since 3.1.5
|
||||
*/
|
||||
trait Line
|
||||
{
|
||||
/**
|
||||
* Set the line number in comments
|
||||
*
|
||||
* @param int $nr The line number
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
private function setLine(int $nr): string
|
||||
{
|
||||
if ($this->debug)
|
||||
{
|
||||
return ' [' . get_called_class() . ' ' . $nr . ']';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
58
libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Tab.php
Normal file
58
libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Tab.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?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;
|
||||
|
||||
|
||||
/**
|
||||
* Adds Tabs
|
||||
*
|
||||
* @since 3.1.5
|
||||
*/
|
||||
trait Tab
|
||||
{
|
||||
/**
|
||||
* Tab/spacer bucket (to speed-up the build)
|
||||
*
|
||||
* @var array
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected $tabSpacerBucket = array();
|
||||
|
||||
/**
|
||||
* Set tab/spacer
|
||||
*
|
||||
* @var string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
protected $tabSpacer = "\t";
|
||||
|
||||
/**
|
||||
* Set the tab/space
|
||||
*
|
||||
* @param int $nr The number of tag/space
|
||||
*
|
||||
* @return string
|
||||
* @since 3.1.5
|
||||
*/
|
||||
public function _t(int $nr) : string
|
||||
{
|
||||
// check if we already have the string
|
||||
if (!isset($this->tabSpacerBucket[$nr]))
|
||||
{
|
||||
// get the string
|
||||
$this->tabSpacerBucket[$nr] = str_repeat($this->tabSpacer, (int) $nr);
|
||||
}
|
||||
// return stored string
|
||||
return $this->tabSpacerBucket[$nr];
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
<html><body bgcolor="#FFFFFF"></body></html>
|
Reference in New Issue
Block a user