joomlatools / joomlatools-framework

Modern PHP extension framework (for Joomla)
https://www.joomlatools.com/developer/framework/
GNU General Public License v3.0
19 stars 11 forks source link

Wrap Joomla calls #489

Closed ercanozkaya closed 3 years ago

ercanozkaya commented 3 years ago

Wrap Joomla calls in the framework in a class so they can be mocked, replaced, or augmented as needed.

This class wraps Joomla calls in the framework in a class so they can be mocked, replaced, or augmented as needed. For example JApplication instance can be reached with $this->getObject('joomla')->app or you can get the database driver with$this->getObject('joomla')->db`

Certain Joomla classes with static methods such as JPluginHelper or JHtml are proxied with anonymous classes. For example $this->getObject('joomla')->pluginHelper->importPlugin('user'); calls JPluginHelper::importPlugin

Constants such as JPATH_ROOT can be reached with $this->getObject('joomla')->getPath('root')

Some methods like isSite, isAdmin, or isDebug are provided for convenience.

See tests below to understand how to use the class better:


$joomla = KObjectManager::getInstance()->getObject('joomla');

assert($joomla instanceof ComKoowaJoomla);
assert($joomla->app instanceof \Joomla\CMS\Application\BaseApplication);
assert($joomla->application instanceof \Joomla\CMS\Application\BaseApplication);
assert($joomla->config === JFactory::getConfig());
assert($joomla->database instanceof \JDatabaseDriver);
assert($joomla->database === $joomla->db);
assert($joomla->document instanceof \Joomla\CMS\Document\Document);
assert($joomla->editor('none') instanceof \Joomla\CMS\Editor\Editor);
assert($joomla->eventDispatcher instanceof JEventDispatcher);
assert($joomla->language === JFactory::getLanguage());
assert($joomla->registry instanceof JRegistry);
assert($joomla->session === JFactory::getSession());
assert($joomla->user instanceof JUser);
assert($joomla->userTable instanceof \Joomla\CMS\Table\User);
assert($joomla->user('manager') === JFactory::getUser('manager'));

assert($joomla->config->get('caching') === JFactory::getConfig()->get('caching'));
assert($joomla->config->get('sitename') === JFactory::getConfig()->get('sitename'));

assert($joomla->uri->getInstance() === JUri::getInstance());
assert($joomla->uri->getInstance()->isSsl() === JFactory::getUri()->isSsl());
assert($joomla->uri->root() === JUri::root());

assert($joomla->route('index.php?option=com_docman') === JRoute::_('index.php?option=com_docman'));
assert($joomla->route('administrator', 'index.php?option=com_docman') === JRoute::link('administrator', 'index.php?option=com_docman'));

assert($joomla->isSite() === true);
assert($joomla->isAdmin() === false);
assert($joomla->isSite() === JFactory::getApplication()->isClient('site'));
assert($joomla->isAdmin() === JFactory::getApplication()->isClient('administrator'));

assert($joomla->getPath('root') === JPATH_ROOT);
assert($joomla->getPath('site') === JPATH_SITE);
assert($joomla->getPath('admin') === JPATH_ADMINISTRATOR);
assert($joomla->getPath('administrator') === JPATH_ADMINISTRATOR);
assert($joomla->getPath('themes') === JPATH_THEMES);

assert($joomla->applicationHelper->stringURLSafe('foo##') === 'foo');
assert($joomla->componentHelper->filterText('<script>foo</script>') === 'foo');

$article = new stdClass();
$article->id = 234;
$article->text = 'ercan@timble.net';
$params = new stdClass();

$joomla->app->triggerEvent('onContentPrepare', ['com_content.article', &$article, &$params]);

assert(!empty($article->jcfields));

assert($joomla->htmlHelper->_('date') === JHtml::_('date'));
assert($joomla->access->getGroupTitle(1) === JAccess::getGroupTitle(1));

assert($joomla->userHelper->getUserId('manager') === JUserHelper::getUserId('manager'));

assert(gettype($joomla->moduleHelper->getModules('position-7')) === 'array');

assert($joomla->pluginHelper->isEnabled('system','joomlatools') === true);
johanjanssens commented 3 years ago

Marking this as invalid for implementation see: #491