generated from yiisoft/package-template
/
ConfigPaths.php
72 lines (61 loc) · 2.23 KB
/
ConfigPaths.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace Yiisoft\Config;
use Yiisoft\Config\Composer\Options;
use function strlen;
use function substr;
use function trim;
/**
* Store the configuration paths necessary for using the {@see Config} instance.
*/
final class ConfigPaths
{
private string $configPath;
private string $vendorPath;
/**
* @param string $rootPath The absolute path to the project root where `composer.json` is located.
* @param string|null $configDirectory The relative path to the configuration storage location.
* @param string|null $vendorDirectory The relative path to the vendor directory.
*/
public function __construct(
private string $rootPath,
string $configDirectory = null,
string $vendorDirectory = null,
) {
$configDirectory = trim($configDirectory ?? Options::DEFAULT_CONFIG_DIRECTORY, '/');
$this->configPath = $rootPath . ($configDirectory === '' ? '' : "/$configDirectory");
$vendorDirectory = trim($vendorDirectory ?? Options::DEFAULT_VENDOR_DIRECTORY, '/');
$this->vendorPath = $rootPath . ($vendorDirectory === '' ? '' : "/$vendorDirectory");
}
/**
* Returns the absolute path to the configuration file.
*
* @param string $file Config file.
* @param string $package Name of the package. {@see Options::ROOT_PACKAGE_NAME} stands for the root package.
*
* @return string The absolute path to the configuration file.
*/
public function absolute(string $file, string $package = Options::ROOT_PACKAGE_NAME): string
{
if ($package === Options::ROOT_PACKAGE_NAME) {
return "$this->configPath/$file";
}
if ($package === Options::VENDOR_OVERRIDE_PACKAGE_NAME) {
return "$this->vendorPath/$file";
}
return "$this->vendorPath/$package/$file";
}
/**
* Returns the relative path to the configuration file.
*
* @param string $file Config file.
*
* @return string The relative path to the configuration file.
*/
public function relative(string $file): string
{
return str_starts_with($file, "$this->rootPath/")
? substr($file, strlen("$this->rootPath/"))
: $file;
}
}