TYPO3  7.6
class-alias-loader/src/Config.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\ClassAliasLoader;
3 
4 /*
5  * This file is part of the class alias loader package.
6  *
7  * (c) Helmut Hummel <info@helhum.io>
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12 
13 use Composer\IO\IOInterface;
14 use Composer\IO\NullIO;
15 use Composer\Package\PackageInterface;
16 
20 class Config
21 {
27  protected $config = array(
28  'class-alias-maps' => null,
29  'always-add-alias-loader' => false,
30  'autoload-case-sensitivity' => true
31  );
32 
36  protected $IO;
37 
42  public function __construct(PackageInterface $package, IOInterface $IO = null)
43  {
44  $this->IO = $IO ?: new NullIO();
45  $this->setAliasLoaderConfigFromPackage($package);
46  }
47 
52  public function get($configKey)
53  {
54  if (empty($configKey)) {
55  throw new \InvalidArgumentException('Configuration key must not be empty', 1444039407);
56  }
57  // Extract parts of the path
58  $configKey = str_getcsv($configKey, '.');
59  // Loop through each part and extract its value
60  $value = $this->config;
61  foreach ($configKey as $segment) {
62  if (array_key_exists($segment, $value)) {
63  // Replace current value with child
64  $value = $value[$segment];
65  } else {
66  return null;
67  }
68  }
69  return $value;
70  }
71 
72 
76  protected function setAliasLoaderConfigFromPackage(PackageInterface $package)
77  {
78  $extraConfig = $this->handleDeprecatedConfigurationInPackage($package);
79  if (isset($extraConfig['typo3/class-alias-loader']['class-alias-maps'])) {
80  $this->config['class-alias-maps'] = (array)$extraConfig['typo3/class-alias-loader']['class-alias-maps'];
81  }
82  if (isset($extraConfig['typo3/class-alias-loader']['always-add-alias-loader'])) {
83  $this->config['always-add-alias-loader'] = (bool)$extraConfig['typo3/class-alias-loader']['always-add-alias-loader'];
84  }
85  if (isset($extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity'])) {
86  $this->config['autoload-case-sensitivity'] = (bool)$extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity'];
87  }
88  }
89 
96  protected function handleDeprecatedConfigurationInPackage(PackageInterface $package)
97  {
98  $extraConfig = $package->getExtra();
99  $messages = array();
100  if (!isset($extraConfig['typo3/class-alias-loader'])) {
101  if (isset($extraConfig['helhum/class-alias-loader'])) {
102  $extraConfig['typo3/class-alias-loader'] = $extraConfig['helhum/class-alias-loader'];
103  $messages[] = sprintf(
104  '<warning>The package "%s" uses "helhum/class-alias-loader" section to define class alias maps, which is deprecated. Please use "typo3/class-alias-loader" instead!</warning>',
105  $package->getName()
106  );
107  } else {
108  $extraConfig['typo3/class-alias-loader'] = array();
109  if (isset($extraConfig['class-alias-maps'])) {
110  $extraConfig['typo3/class-alias-loader']['class-alias-maps'] = $extraConfig['class-alias-maps'];
111  $messages[] = sprintf(
112  '<warning>The package "%s" uses "class-alias-maps" section on top level, which is deprecated. Please move this config below the top level key "typo3/class-alias-loader" instead!</warning>',
113  $package->getName()
114  );
115  }
116  if (isset($extraConfig['autoload-case-sensitivity'])) {
117  $extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity'] = $extraConfig['autoload-case-sensitivity'];
118  $messages[] = sprintf(
119  '<warning>The package "%s" uses "autoload-case-sensitivity" section on top level, which is deprecated. Please move this config below the top level key "typo3/class-alias-loader" instead!</warning>',
120  $package->getName()
121  );
122  }
123  }
124  }
125  if (!empty($messages)) {
126  $this->IO->writeError($messages);
127  }
128  return $extraConfig;
129  }
130 
131 }