52
0
plg_extension_componentbuil.../componentbuilderheaderscompiler.php

733 lines
20 KiB
PHP
Raw Normal View History

2021-10-14 14:31:41 +00:00
<?php
2021-12-03 02:46:17 +00:00
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <https://dev.vdm.io>
2022-08-20 16:38:51 +00:00
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
2021-12-03 02:46:17 +00:00
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
2021-10-14 14:31:41 +00:00
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Plugin\CMSPlugin;
2023-10-18 07:28:04 +00:00
use Joomla\Registry\Registry;
2024-03-11 14:14:23 +00:00
use VDM\Joomla\Componentbuilder\Compiler\Factory;
2023-10-18 07:28:04 +00:00
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\JsonHelper;
use VDM\Joomla\Utilities\StringHelper;
2024-03-11 14:14:23 +00:00
use VDM\Joomla\Utilities\GuidHelper;
2023-10-18 07:28:04 +00:00
use VDM\Joomla\Utilities\String\NamespaceHelper;
2021-10-14 14:31:41 +00:00
/**
* Extension - Componentbuilder Headers Compiler plugin.
*
* @package ComponentbuilderHeadersCompiler
2024-03-11 14:14:23 +00:00
* @since 3.0.0
2021-10-14 14:31:41 +00:00
*/
class PlgExtensionComponentbuilderHeadersCompiler extends CMSPlugin
{
2021-12-08 20:42:13 +00:00
/**
2021-10-14 14:31:59 +00:00
* Global switch to see if a file has custom headers.
2021-10-14 14:31:41 +00:00
*
* @var boolean
* @since 1.0.0
*/
2021-12-08 20:42:13 +00:00
protected $loadHeaders = false;
2021-10-14 14:31:41 +00:00
/**
2021-12-08 20:42:13 +00:00
* The active headers
2021-10-14 14:31:41 +00:00
*
* @var array
2021-12-08 20:42:13 +00:00
* @since 1.0.8
2021-10-14 14:31:41 +00:00
*/
2024-03-11 14:14:23 +00:00
protected $activeHeaders = [];
2021-10-14 14:31:41 +00:00
2021-12-03 02:46:17 +00:00
/**
* The compiler placeholders values
*
* @var array
* @since 1.0.6
*/
2021-12-08 20:42:13 +00:00
protected $placeholders = array();
2021-12-03 02:46:17 +00:00
/**
* The powers to include in project
*
* @var array
* @since 1.0.6
*/
2021-12-08 20:42:13 +00:00
protected $linkedPowers = array();
2021-12-03 02:46:17 +00:00
2021-10-14 14:31:41 +00:00
/**
* The Targets
*
* @var array
2021-12-08 20:42:13 +00:00
* @since 1.0.8
2021-10-14 14:31:41 +00:00
*/
2021-10-14 14:31:46 +00:00
protected $targets = array(
2021-12-08 20:42:13 +00:00
'admin_view_headers' =>
2021-10-14 14:31:46 +00:00
array(
'add_admin_view_model' => array(
'field' => 'admin_view_model',
'context' => 'admin.view.model',
'view' => 'name_single'
),
2021-12-08 20:42:13 +00:00
'add_admin_view' => array(
2021-10-14 14:32:05 +00:00
'field' => 'admin_view',
'context' => 'admin.view',
'view' => 'name_single'
),
2021-12-08 20:42:13 +00:00
'add_admin_view_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'admin_view_html',
'context' => 'admin.view.html',
'view' => 'name_single'
),
2021-12-08 20:42:13 +00:00
'add_site_admin_view_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'site_admin_view_html',
'context' => 'site.admin.view.html',
'view' => 'name_single'
),
2021-10-14 14:32:05 +00:00
'add_admin_view_controller' => array(
'field' => 'admin_view_controller',
'context' => 'admin.view.controller',
'view' => 'name_single'
),
2021-12-08 20:42:13 +00:00
'add_ajax_model' => array(
'field' => 'ajax_model',
'context' => 'ajax.admin.model',
'view' => 'ajax'
),
2021-10-14 14:31:46 +00:00
'add_admin_views_model' => array(
'field' => 'admin_views_model',
'context' => 'admin.views.model',
'view' => 'name_list'
2021-10-14 14:32:05 +00:00
),
2021-12-08 20:42:13 +00:00
'add_admin_views' => array(
2021-10-14 14:32:05 +00:00
'field' => 'admin_views',
'context' => 'admin.views',
'view' => 'name_list'
),
2021-12-08 20:42:13 +00:00
'add_admin_views_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'admin_views_html',
'context' => 'admin.views.html',
'view' => 'name_list'
),
2021-10-14 14:32:05 +00:00
'add_admin_views_controller' => array(
'field' => 'admin_views_controller',
'context' => 'admin.views.controller',
'view' => 'name_list'
),
'add_import_custom_controller' => array(
'field' => 'import_custom_controller',
'context' => 'import.custom.controller',
'view' => 'name_list'
),
'add_import_custom_model' => array(
'field' => 'import_custom_model',
'context' => 'import.custom.model',
'view' => 'name_list'
2021-10-14 14:31:46 +00:00
)
2021-10-14 14:31:41 +00:00
),
2021-12-08 20:42:13 +00:00
'site_view_headers' =>
2021-10-14 14:31:46 +00:00
array(
2021-10-14 14:32:05 +00:00
'add_site_view_model' => array(
'field' => 'site_view_model',
'context' => 'site.view.model',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_site_view' => array(
2021-10-14 14:32:05 +00:00
'field' => 'site_view',
'context' => 'site.view',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_site_view_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'site_view_html',
'context' => 'site.view.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_site_view_controller' => array(
'field' => 'site_view_controller',
'context' => 'site.view.controller',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
'add_site_views_model' => array(
'field' => 'site_views_model',
'context' => 'site.views.model',
'view' => 'code'
2021-10-14 14:32:05 +00:00
),
2021-12-08 20:42:13 +00:00
'add_site_views' => array(
2021-10-14 14:32:05 +00:00
'field' => 'site_views',
'context' => 'site.views',
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_site_views_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'site_views_html',
'context' => 'site.views.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_site_views_controller' => array(
'field' => 'site_views_controller',
'context' => 'site.views.controller',
'view' => 'code'
2021-12-08 20:42:13 +00:00
),
'add_ajax_model' => array(
'field' => 'ajax_model',
'context' => 'ajax.site.model',
'view' => 'ajax'
2021-10-14 14:31:46 +00:00
)
2021-10-14 14:31:53 +00:00
),
2021-12-08 20:42:13 +00:00
'custom_admin_view_headers' =>
2021-10-14 14:31:53 +00:00
array(
2021-10-14 14:32:05 +00:00
'add_custom_admin_view_model' => array(
'field' => 'custom_admin_view_model',
'context' => 'custom.admin.view.model',
2021-10-14 14:31:53 +00:00
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_custom_admin_view' => array(
2021-10-14 14:32:05 +00:00
'field' => 'custom_admin_view',
'context' => 'custom.admin.view',
2021-10-14 14:31:53 +00:00
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_custom_admin_view_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'custom_admin_view_html',
'context' => 'custom.admin.view.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_custom_admin_view_controller' => array(
'field' => 'custom_admin_view_controller',
'context' => 'custom.admin.view.controller',
2021-10-14 14:31:53 +00:00
'view' => 'code'
2022-01-10 14:13:13 +00:00
),
'add_custom_admin_views_model' => array(
'field' => 'custom_admin_views_model',
'context' => 'custom.admin.views.model',
'view' => 'code'
),
'add_custom_admin_views' => array(
'field' => 'custom_admin_views',
'context' => 'custom.admin.views',
'view' => 'code'
),
'add_custom_admin_views_html' => array(
'field' => 'custom_admin_views_html',
'context' => 'custom.admin.views.html',
'view' => 'code'
),
'add_custom_admin_views_controller' => array(
'field' => 'custom_admin_views_controller',
'context' => 'custom.admin.views.controller',
'view' => 'code'
),
'add_ajax_model' => array(
'field' => 'ajax_model',
'context' => 'ajax.admin.model',
'view' => 'ajax'
)
),
'dynamic_get_headers' =>
array(
'add_site_view_model' => array(
'field' => 'site_view_model',
'context' => 'site.view.model',
'view' => 'code'
),
'add_site_view' => array(
'field' => 'site_view',
'context' => 'site.view',
'view' => 'code'
),
'add_site_view_html' => array(
'field' => 'site_view_html',
'context' => 'site.view.html',
'view' => 'code'
),
'add_site_view_controller' => array(
'field' => 'site_view_controller',
'context' => 'site.view.controller',
'view' => 'code'
),
'add_site_views_model' => array(
'field' => 'site_views_model',
'context' => 'site.views.model',
'view' => 'code'
),
'add_site_views' => array(
'field' => 'site_views',
'context' => 'site.views',
'view' => 'code'
),
'add_site_views_html' => array(
'field' => 'site_views_html',
'context' => 'site.views.html',
'view' => 'code'
),
'add_site_views_controller' => array(
'field' => 'site_views_controller',
'context' => 'site.views.controller',
'view' => 'code'
),
'add_custom_admin_view_model' => array(
'field' => 'custom_admin_view_model',
'context' => 'custom.admin.view.model',
'view' => 'code'
),
'add_custom_admin_view' => array(
'field' => 'custom_admin_view',
'context' => 'custom.admin.view',
'view' => 'code'
),
'add_custom_admin_view_html' => array(
'field' => 'custom_admin_view_html',
'context' => 'custom.admin.view.html',
'view' => 'code'
),
'add_custom_admin_view_controller' => array(
'field' => 'custom_admin_view_controller',
'context' => 'custom.admin.view.controller',
'view' => 'code'
2021-10-14 14:31:53 +00:00
),
'add_custom_admin_views_model' => array(
'field' => 'custom_admin_views_model',
'context' => 'custom.admin.views.model',
'view' => 'code'
2021-10-14 14:32:05 +00:00
),
2021-12-08 20:42:13 +00:00
'add_custom_admin_views' => array(
2021-10-14 14:32:05 +00:00
'field' => 'custom_admin_views',
'context' => 'custom.admin.views',
'view' => 'code'
),
2021-12-08 20:42:13 +00:00
'add_custom_admin_views_html' => array(
2021-12-03 02:46:17 +00:00
'field' => 'custom_admin_views_html',
'context' => 'custom.admin.views.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_custom_admin_views_controller' => array(
'field' => 'custom_admin_views_controller',
'context' => 'custom.admin.views.controller',
'view' => 'code'
2021-12-08 20:42:13 +00:00
),
'add_ajax_model' => array(
'field' => 'ajax_model',
'context' => 'ajax.admin.model',
'view' => 'ajax'
2021-10-14 14:31:53 +00:00
)
2021-10-14 14:31:59 +00:00
),
2021-12-08 20:42:13 +00:00
'component_dashboard_headers' =>
2021-10-14 14:31:59 +00:00
array(
2021-12-08 20:42:13 +00:00
'add_dashboard_model' => array(
'field' => 'dashboard_model',
'context' => 'dashboard.model',
'view' => 'dashboard'
),
'add_dashboard_view' => array(
'field' => 'dashboard_view',
'context' => 'dashboard.view',
'view' => 'dashboard'
),
'add_dashboard_view_html' => array(
'field' => 'dashboard_view_html',
'context' => 'dashboard.view.html',
'view' => 'dashboard'
),
'add_dashboard_controller' => array(
'field' => 'dashboard_controller',
'context' => 'dashboard.controller',
'view' => 'dashboard'
),
'add_ajax_model' => array(
'field' => 'ajax_model',
'context' => 'ajax.admin.model',
'view' => 'ajax'
)
),
'joomla_component_headers' =>
array(
'add_admin_component' => array(
2021-10-14 14:31:59 +00:00
'field' => 'admin_component',
'context' => 'admin.component',
'view' => 'admin'
),
'add_site_component' => array(
'field' => 'site_component',
'context' => 'site.component',
'view' => 'site'
),
2021-12-08 20:42:13 +00:00
'add_admin_helper' => array(
2021-10-14 14:31:59 +00:00
'field' => 'admin_helper',
'context' => 'admin.helper',
'view' => 'admin'
),
2021-12-08 20:42:13 +00:00
'add_site_helper' => array(
2021-10-14 14:31:59 +00:00
'field' => 'site_helper',
'context' => 'site.helper',
'view' => 'site'
)
2021-10-14 14:31:41 +00:00
)
2021-12-08 20:42:13 +00:00
);
2021-10-14 14:31:41 +00:00
/**
* Event Triggered in the compiler [on Before Model View Data]
*
* @return void
*
* @since 1.0
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onBeforeModelViewData(&$view)
2021-12-08 20:42:13 +00:00
{
// check that the params are set
if (isset($view->params))
2021-10-14 14:31:46 +00:00
{
2021-12-08 20:42:13 +00:00
// add the headers for the Admin Views
$this->setHeaders($view->params, $view, 'admin_view_headers');
2021-10-14 14:31:46 +00:00
}
}
/**
* Event Triggered in the compiler [on Before Model Custom View Data]
*
* @return void
*
2021-12-08 20:42:13 +00:00
* @since 1.0.2
2021-10-14 14:31:46 +00:00
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onBeforeModelCustomViewData(&$view, &$id, &$table)
2021-12-08 20:42:13 +00:00
{
// check that the params are set
if (isset($view->params))
2021-10-14 14:31:41 +00:00
{
2021-12-08 20:42:13 +00:00
// add the headers for the Site Views
$this->setHeaders($view->params, $view, 'site_view_headers');
// add the headers for the Custom Admin Views
$this->setHeaders($view->params, $view, 'custom_admin_view_headers');
2021-10-14 14:31:41 +00:00
}
2021-12-08 20:42:13 +00:00
}
2022-01-10 14:47:57 +00:00
/**
* Event Triggered in the compiler [on Before Model Dynamic Get Data]
*
* @return void
*
* @since 1.0.10
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onBeforeModelDynamicGetData(&$dynamicGet, &$id, &$code, &$area)
2022-01-10 14:47:57 +00:00
{
// check that the params are set
if (isset($dynamicGet->params))
{
// add the headers for the Site Views
$this->setDynamicHeaders($dynamicGet->params, $code, 'dynamic_get_headers');
}
}
2021-12-08 20:42:13 +00:00
/**
* Event Triggered in the compiler [on Before Model Component Data]
*
* @return void
*
* @since 1.0.4
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onBeforeModelComponentData(&$component)
2021-12-08 20:42:13 +00:00
{
// check that the params are set
if (isset($component->params))
{
// add the headers for the Joomla Component
$this->setHeaders($component->params, $component, 'joomla_component_headers');
}
// check that the dashboard params are set
if (isset($component->dashboard_params))
{
// add the headers for the Component Dashboard
$this->setHeaders($component->dashboard_params, $component, 'component_dashboard_headers');
}
}
/**
* Event Triggered in the compiler [on set Class Header]
*
* @return void
*
* @since 1.0
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_setClassHeader(&$event_context, &$view_name, &$headers)
2021-12-08 20:42:13 +00:00
{
if ($this->loadHeaders && isset($this->activeHeaders[$view_name])
2022-09-20 13:07:47 +00:00
&& isset($this->activeHeaders[$view_name][$event_context])
&& is_array($this->activeHeaders[$view_name][$event_context]))
2021-10-14 14:31:53 +00:00
{
2022-09-20 13:07:47 +00:00
// work with the header values as keys
$_headers = array_flip($headers);
// new headers
$new = $this->activeHeaders[$view_name][$event_context];
// now add the new headers
foreach ($new as $n => $header)
2021-10-14 14:31:53 +00:00
{
2022-09-20 13:07:47 +00:00
// if an empty line is found just skip it
// we check if this header is already set
if (empty($header) || isset($_headers[$header]))
2021-10-14 14:31:53 +00:00
{
2021-12-08 20:42:13 +00:00
continue;
}
2022-09-20 13:07:47 +00:00
$headers[] = $header;
2021-10-14 14:31:53 +00:00
}
}
2021-10-14 14:31:41 +00:00
}
/**
2021-12-08 20:42:13 +00:00
* Event Triggered in the compiler [on Before Get Component Data]
2021-10-14 14:31:59 +00:00
*
* @return void
*
2021-12-08 20:42:13 +00:00
* @since 1.0.6
2021-10-14 14:31:59 +00:00
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onBeforeGetComponentData()
2021-12-08 20:42:13 +00:00
{
// get placeholders from the compiler
2024-03-11 14:14:23 +00:00
$this->placeholders = Factory::_('Component.Placeholder')->get();
2021-12-08 20:42:13 +00:00
}
/**
* Event Triggered in the compiler [on After Get Component Data]
*
* @return void
*
* @since 1.0.6
*/
2024-03-02 20:10:30 +00:00
public function jcb_ce_onAfterGetComponentData()
2021-12-08 20:42:13 +00:00
{
// add the powers to the component
2023-10-18 07:28:04 +00:00
if (ArrayHelper::check($this->linkedPowers, true))
2021-12-08 20:42:13 +00:00
{
2024-03-11 14:14:23 +00:00
Factory::_('Power')->load($this->linkedPowers);
2021-12-08 20:42:13 +00:00
}
}
/**
* set the headers
*
* @return void
*
* @since 1.0.8
*/
protected function setHeaders(&$params, &$obj, $key)
2021-10-14 14:31:59 +00:00
{
2021-12-08 20:42:13 +00:00
// add the headers
2023-10-18 07:28:04 +00:00
$params = (JsonHelper::check($params)) ? json_decode($params, true) : $params;
2021-12-08 20:42:13 +00:00
// make sure we have the keys values in the params area
2023-10-18 07:28:04 +00:00
if (ArrayHelper::check($params) && isset($params[$key])
&& ArrayHelper::check($params[$key]))
2021-10-14 14:31:59 +00:00
{
2021-12-08 20:42:13 +00:00
foreach ($this->targets[$key] as $target => $event)
2021-10-14 14:31:59 +00:00
{
2021-12-08 20:42:13 +00:00
if (isset($params[$key][$target])
&& $params[$key][$target] == 1)
2021-10-14 14:31:59 +00:00
{
2021-12-08 20:42:13 +00:00
// get the header string if set
$this->getHeaders(
$params[$key],
$event,
$this->getViewName(
$obj,
$event['view']
)
);
2021-10-14 14:31:59 +00:00
}
}
}
}
2022-01-10 14:47:57 +00:00
/**
* set the dynamic get headers
*
* @return void
*
* @since 1.0.10
*/
protected function setDynamicHeaders($params, $code, $key)
{
// add the headers
2023-10-18 07:28:04 +00:00
$params = (JsonHelper::check($params)) ? json_decode($params, true) : $params;
2022-01-10 14:47:57 +00:00
// make sure we have the keys values in the params area
2023-10-18 07:28:04 +00:00
if (ArrayHelper::check($params) && isset($params[$key])
&& ArrayHelper::check($params[$key]))
2022-01-10 14:47:57 +00:00
{
foreach ($this->targets[$key] as $target => $event)
{
if (isset($params[$key][$target])
&& $params[$key][$target] == 1)
{
// get the header string if set
$this->getHeaders(
$params[$key],
$event,
$code
);
}
}
}
}
2021-12-03 02:46:17 +00:00
/**
2021-12-08 20:42:13 +00:00
* get the headers
2021-12-03 02:46:17 +00:00
*
* @return void
*
* @since 1.0
*/
2021-12-08 20:42:13 +00:00
protected function getHeaders(&$params, &$get, $view_name)
2021-12-03 02:46:17 +00:00
{
// we first check if the value is set
2021-12-08 20:42:13 +00:00
if (isset($params[$get['field']]) || isset($params['power_' . $get['field']]))
2021-12-03 02:46:17 +00:00
{
2021-12-08 20:42:13 +00:00
// start little headers bucket
2023-02-15 00:46:55 +00:00
$headers = [];
// load the headers if power
2023-10-18 07:28:04 +00:00
if (isset($params['power_' . $get['field']]) && ArrayHelper::check($params['power_' . $get['field']], true)
&& ($powers = $this->getPowers($params['power_' . $get['field']])) !== null)
2023-02-15 00:46:55 +00:00
{
foreach ($powers as $power)
{
$power = trim($power);
$headers[$power] = $power;
}
}
2021-12-03 02:46:17 +00:00
// load the headers if text
2023-10-18 07:28:04 +00:00
if (isset($params[$get['field']]) && StringHelper::check($params[$get['field']]))
2021-12-03 02:46:17 +00:00
{
2023-10-18 07:28:04 +00:00
if (($_headers = explode(PHP_EOL, $params[$get['field']])))
2022-09-20 13:07:47 +00:00
{
foreach ($_headers as $header)
{
$header = trim($header);
if (empty($header))
{
continue;
}
$headers[$header] = $header;
}
}
2021-12-03 02:46:17 +00:00
}
2023-02-15 00:46:55 +00:00
2021-12-08 20:42:13 +00:00
// check if we found some header values
2023-10-18 07:28:04 +00:00
if (ArrayHelper::check($headers, true))
2021-12-03 02:46:17 +00:00
{
2021-12-08 20:42:13 +00:00
// activate the load of the headers
$this->loadHeaders = true;
// check if this active header is already set
if (!isset($this->activeHeaders[$view_name][$get['context']]))
{
// start the active header
2022-09-20 13:07:47 +00:00
$this->activeHeaders[$view_name][$get['context']] = [];
2021-12-08 20:42:13 +00:00
}
2022-09-20 13:07:47 +00:00
// load the found headers and avoid adding the same header twice
foreach ($headers as $header)
2021-12-08 20:42:13 +00:00
{
2022-09-20 13:07:47 +00:00
$header = trim($header);
$this->activeHeaders[$view_name][$get['context']][$header] = $header;
2021-12-08 20:42:13 +00:00
}
2021-12-03 02:46:17 +00:00
}
}
}
/**
2021-12-08 20:42:13 +00:00
* get the view name
2021-12-03 02:46:17 +00:00
*
* @return string
*
2021-12-08 20:42:13 +00:00
* @since 1.0.8
*/
protected function getViewName(&$view, &$get)
{
if ($get === 'site' || $get === 'admin' || $get === 'ajax' || $get === 'dashboard')
{
// static key name
return $get;
}
elseif (isset($view->{$get}))
{
2023-10-18 07:28:04 +00:00
return StringHelper::safe(
2021-12-08 20:42:13 +00:00
$view->{$get}
);
}
return '_error';
}
/**
* get the powers header use strings
*
2022-09-20 13:07:47 +00:00
* @return array|null
2021-12-08 20:42:13 +00:00
*
2021-12-03 02:46:17 +00:00
* @since 1.0.6
*/
2022-09-20 13:07:47 +00:00
protected function getPowers($rows): ?array
2021-12-03 02:46:17 +00:00
{
2022-05-18 11:08:14 +00:00
// load the active powers
$powers = array_filter(
// get the power namespace
array_map(function ($row) {
2024-03-11 14:14:23 +00:00
if (($power = GuidHelper::item($row['power'], 'power', ['a.guid', 'a.namespace'])) !== null)
2022-05-18 11:08:14 +00:00
{
2023-10-18 07:28:04 +00:00
$power->build = (int) $row['build'];
$power->as = (string) $row['as'];
return $power;
}
2024-03-11 14:14:23 +00:00
elseif (Factory::_('Superpower')->load($row['power'], ['remote']))
2023-10-18 07:28:04 +00:00
{
2024-03-11 14:14:23 +00:00
if (($power = GuidHelper::item($row['power'], 'power', ['a.guid', 'a.namespace'])) !== null)
2023-10-18 07:28:04 +00:00
{
$power->build = (int) $row['build'];
$power->as = (string) $row['as'];
return $power;
}
2022-05-18 11:08:14 +00:00
}
return false;
}, $rows),
// check that we have valid powers
function ($row) {
return is_object($row) && isset($row->guid);
}
);
2021-12-03 02:46:17 +00:00
// add to active powers
2023-10-18 07:28:04 +00:00
if (ArrayHelper::check($powers))
2021-12-03 02:46:17 +00:00
{
// convert the dots to namespace
2022-09-20 13:07:47 +00:00
return array_map(function ($power) {
2022-05-18 11:08:14 +00:00
// add to compiler (to build)
if ($power->build != 6)
{
// secure that always will remain always even if only set that way once
if (empty($this->linkedPowers[$power->guid]) || $power->build == 1)
{
$this->linkedPowers[$power->guid] = $power->build;
}
}
// build the namespace
2023-10-18 07:28:04 +00:00
$namespace = NamespaceHelper::safe(
2021-12-03 02:46:17 +00:00
str_replace(
array_keys($this->placeholders),
array_values($this->placeholders),
2022-05-18 11:08:14 +00:00
str_replace('.', '\\', $power->namespace)
2021-12-03 02:46:17 +00:00
)
);
2022-05-18 11:08:14 +00:00
// check if it has an AS option
2023-10-18 07:28:04 +00:00
if (StringHelper::check($power->as) && $power->as !== 'default')
2022-05-18 11:08:14 +00:00
{
return 'use ' . $namespace . ' as ' . $power->as . ';';
}
return 'use ' . $namespace . ';';
}, $powers);
2021-10-14 14:31:41 +00:00
}
2021-12-03 02:46:17 +00:00
2022-09-20 13:07:47 +00:00
return null;
2024-03-11 14:14:23 +00:00
}
2021-10-14 14:31:41 +00:00
}