LudwigServiceProvider.php in Ludwig 8
File
src/LudwigServiceProvider.php
View source
<?php
namespace Drupal\ludwig;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
class LudwigServiceProvider extends ServiceProviderBase {
public function register(ContainerBuilder $container) {
$root = \Drupal::hasService('app.root') ? \Drupal::root() : DRUPAL_ROOT;
$package_manager = new PackageManager($root);
$namespaces = $container
->getParameter('container.namespaces');
foreach ($package_manager
->getPackages() as $package_name => $package) {
if ($package['installed']) {
if ($package['resource'] == 'psr-4' || $package['resource'] == 'psr-0') {
$namespace = $package['namespace'];
if (isset($namespaces[$namespace])) {
if (is_string($namespaces[$namespace])) {
$old_paths = [];
$old_paths[] = $namespaces[$namespace];
}
elseif (is_array($namespaces[$namespace])) {
$old_paths = $namespaces[$namespace];
}
}
else {
$old_paths = [];
}
$new_paths = [];
if (!empty($package['paths'])) {
foreach ($package['paths'] as $path) {
!empty($path) ? $new_paths[] = $package['path'] . '/' . $path : ($new_paths[] = $package['path']);
}
}
$return_paths = array_unique(array_merge($old_paths, $new_paths));
if (count($return_paths) == 1) {
$namespaces[$namespace] = $return_paths[0];
}
elseif (count($return_paths) > 1) {
$namespaces[$namespace] = $return_paths;
}
}
}
}
$container
->setParameter('container.namespaces', $namespaces);
}
}