PathResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * PathResolver.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Helpers
10
 * @since          1.0.0
11
 *
12
 * @date           19.06.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Helpers;
18
19
use Nette;
20
use Nette\Utils;
21
22
use IPub\Packages\Exceptions;
23
use IPub\Packages\Repository;
24
25
/**
26
 * Package path resolver
27
 *
28
 * @package        iPublikuj:Packages!
29
 * @subpackage     Helpers
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
final class PathResolver
34
{
35
	/**
36
	 * Implement nette smart magic
37
	 */
38 1
	use Nette\SmartObject;
39
40
	/**
41
	 * @var Repository\IRepository
42
	 */
43
	private $repository;
44
45
	/**
46
	 * @param Repository\IRepository $repository
47
	 */
48
	public function __construct(Repository\IRepository $repository)
49
	{
50 1
		$this->repository = $repository;
51 1
	}
52
53
	/**
54
	 * Expands @foo/path/....
55
	 *
56
	 * @param string $path
57
	 * @param string|NULL $localPrefix
58
	 *
59
	 * @return string
60
	 */
61
	public function expandPath(string $path, string $localPrefix = NULL) : string
62
	{
63
		$path = Utils\Strings::replace($path, '~\\\~', '/');
64
65
		if (substr($path, 0, 1) !== '@') {
66
			return $path;
67
		}
68
69
		$pos = strpos($path, '/');
70
71
		if ($pos) {
72
			$package = substr($path, 1, $pos - 1);
73
74
		} else {
75
			$package = substr($path, 1);
76
		}
77
78
		$package = str_replace('.', '/', $package);
79
80
		if (!$this->repository->findPackage($package)) {
81
			throw new Exceptions\InvalidArgumentException(sprintf('Package \'%s\' does not exist.', $package));
82
		}
83
84
		$path = $this->repository->findPackage($package)->getPath() . ($localPrefix ? '/' . $localPrefix : '') . ($pos ? substr($path, $pos) : '');
85
86
		return Utils\Strings::replace($path, '~\\\~', '/');
87
	}
88
}
89