- md5
- 25dd12ad575dde80641d00687f6bd9f6
- sha1
- fe90d614cd8026e899313cdddaf8693b1eb49a5f
- sha256
- 6c92f517c40de20664a2cd533494a4924bdf9e2e6ce687ad054fc590623d299b
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
<?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__
*/
protected $file = JPATH_LIBRARIES . '/autoload_psr4.php';
/**
* Check if the file exists
*
* @return bool
*
* @since __DEPLOY_VERSION__
*/
public function exists()
{
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__
*/
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())
{
$this->create();
}
}
/**
* Create the namespace file
*
* @return bool
*
* @since __DEPLOY_VERSION__
*/
public function create()
{
$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);
}
}
$this->writeNamespaceFile($elements);
return true;
}
/**
* Load the PSR4 file
*
* @return bool
*
* @since __DEPLOY_VERSION__
*/
public function load()
{
if (!$this->exists())
{
// We can't continue here
if (!JFactory::getDbo()->connected())
{
return false;
}
$this->create();
}
$map = require $this->file;
$loader = include JPATH_LIBRARIES . '/vendor/autoload.php';
foreach ($map as $namespace => $path)
{
$loader->setPsr4($namespace, $path);
}
return true;
}
/**
* Write the Namespace mapping file
*
* @param array $elements Array of elements
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function writeNamespaceFile($elements)
{
$content = array();
$content[] = "<?php";
$content[] = 'defined(\'_JEXEC\') or die;';
$content[] = 'return array(';
foreach ($elements as $namespace => $paths)
{
$pathString = '';
foreach ($paths as $path)
{
$pathString .= '"' . $path . '",';
}
$content[] = "\t'" . $namespace . "'" . ' => array(JPATH_ROOT . ' . $pathString . '),';
}
$content[] = ');';
file_put_contents($this->file, implode("\n", $content));
}
/**
* Get all namespaced extensions from the database
*
* @return mixed|false
*
* @since __DEPLOY_VERSION__
*/
protected function getNamespacedExtensions()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$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;
}
}
No other release has this exact content. See every version of this file.