29
0
mirror of https://github.com/joomla/joomla-cms.git synced 2024-06-27 07:33:41 +00:00
cms/libraries/namespacemap.php

160 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
* @package Joomla.Libraries
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Class JNamespaceMap
*
* @since __DEPLOY_VERSION__
*/
class JNamespacePsr4Map
{
/**
* Path to the autoloader
*
* @var string
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:49:13 +00:00
protected $file = '';
2017-06-03 17:30:40 +00:00
/**
* Constructor. For PHP 5.5 compatibility we must set the file property like this
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:49:13 +00:00
public function __construct()
{
$this->file = JPATH_LIBRARIES . '/autoload_psr4.php';
}
/**
* Check if the file exists
*
* @return bool
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:45:58 +00:00
public function exists()
{
2017-06-01 08:45:58 +00:00
if (!file_exists($this->file))
{
return false;
}
return true;
}
/**
* Check if the namespace mapping file exists, if not create it
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:45:58 +00:00
public function ensureMapFileExists()
{
// Ensure that the database is connected (because it isn't in the installer where this function gets called from
// CMSApplication
if (!$this->exists() && JFactory::getDbo()->connected())
{
2017-06-01 08:45:58 +00:00
$this->create();
}
}
/**
* Create the namespace file
*
* @return bool
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:45:58 +00:00
public function create()
{
2017-06-01 08:45:58 +00:00
$extensions = $this->getNamespacedExtensions();
$elements = array();
foreach ($extensions as $extension)
{
$element = $extension->element;
$baseNamespace = str_replace("\\", "\\\\", $extension->namespace);
if (file_exists(JPATH_ADMINISTRATOR . '/components/' . $element))
{
$elements[$baseNamespace . '\\\\Administrator'] = array('/administrator/components/' . $element);
}
if (file_exists(JPATH_ROOT . '/components/' . $element))
{
$elements[$baseNamespace . '\\\\Site'] = array('/components/' . $element);
}
}
2017-06-01 08:45:58 +00:00
$this->writeNamespaceFile($elements);
return true;
}
/**
* Write the Namespace mapping file
*
* @param array $elements Array of elements
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:45:58 +00:00
protected function writeNamespaceFile($elements)
{
$content = array();
$content[] = "<?php";
$content[] = 'return array(';
foreach ($elements as $namespace => $paths)
{
$pathString = '';
foreach ($paths as $path)
{
$pathString .= '"' . $path . '",';
}
$content[] = "\t'" . $namespace . "'" . ' => array(JPATH_ROOT . ' . $pathString . '),';
}
$content[] = ');';
2017-06-01 08:45:58 +00:00
file_put_contents($this->file, implode("\n", $content));
}
/**
* Get all namespaced extensions from the database
*
* @return mixed|false
*
* @since __DEPLOY_VERSION__
*/
2017-06-01 08:45:58 +00:00
protected function getNamespacedExtensions()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
2017-05-31 23:04:56 +00:00
$query->select($db->quoteName(array('extension_id', 'element', 'namespace')))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('namespace') . ' IS NOT NULL AND ' . $db->quoteName('namespace') . ' != ""');
$db->setQuery($query);
$extensions = $db->loadObjectList();
return $extensions;
}
}