ComposerJson.php in X Autoload 7.5
File
src/Discovery/ComposerJson.php
View source
<?php
namespace Drupal\xautoload\Discovery;
use Drupal\xautoload\Adapter\ClassFinderAdapter;
class ComposerJson {
protected $data;
protected $pathPrefix;
static function createFromFile($file) {
if (!file_exists($file)) {
throw new \Exception("File '{$file}' does not exist.");
}
$json = file_get_contents($file);
$data = json_decode($json, TRUE);
if (NULL === $data && JSON_ERROR_NONE !== json_last_error()) {
throw new \Exception("Invalid json in '{$file}'.");
}
return self::createFromData($data, dirname($file) . '/');
}
static function createFromData($data, $path_prefix) {
return empty($data['target-dir']) ? new self($data, $path_prefix) : new ComposerJsonTargetDir($data, $path_prefix);
}
protected function __construct(array $data, $path_prefix) {
$this->data = $data;
$this->pathPrefix = $path_prefix;
}
function writeToAdapter(ClassFinderAdapter $adapter) {
$data = $this->data;
if (!empty($data['include-path'])) {
$this
->addIncludePaths((array) $data['include-path']);
}
if (!empty($data['autoload']['psr-0'])) {
$map = $this
->transformMultiple($data['autoload']['psr-0']);
$adapter
->addMultiplePsr0($map);
}
if (!empty($data['autoload']['psr-4'])) {
$map = $this
->transformMultiple($data['autoload']['psr-4']);
$adapter
->addMultiplePsr4($map);
}
if (!empty($data['autoload']['classmap'])) {
$this
->addClassmapSources($adapter, (array) $data['autoload']['classmap']);
}
if (!empty($data['autoload']['files'])) {
foreach ($data['autoload']['files'] as $file) {
require $this->pathPrefix . $file;
}
}
}
protected function transformMultiple(array $multiple) {
foreach ($multiple as &$paths) {
$paths = (array) $paths;
foreach ($paths as &$path) {
if ('' === $path || '/' !== $path[0]) {
$path = $this->pathPrefix . $path;
}
}
}
return $multiple;
}
protected function addIncludePaths(array $include_paths) {
foreach ($include_paths as &$path) {
$path = $this->pathPrefix . $path;
}
array_push($include_paths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $include_paths));
}
protected function addClassmapSources($adapter, array $sources_raw) {
foreach ($sources_raw as &$path) {
$path = $this->pathPrefix . $path;
}
$adapter
->addClassmapSources($sources_raw);
}
}