1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\Asset\DI; |
6
|
|
|
|
7
|
|
|
use Nette; |
8
|
|
|
use Symfony; |
9
|
|
|
|
10
|
|
|
final class PackageDefinitionFacade |
11
|
|
|
{ |
12
|
|
|
use Nette\SmartObject; |
13
|
|
|
|
14
|
|
|
/** @var \SixtyEightPublishers\Asset\DI\ReferenceFacade */ |
15
|
|
|
private $referenceFacade; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \SixtyEightPublishers\Asset\DI\ReferenceFacade $referenceFacade |
19
|
|
|
*/ |
20
|
1 |
|
public function __construct(ReferenceFacade $referenceFacade) |
21
|
|
|
{ |
22
|
1 |
|
$this->referenceFacade = $referenceFacade; |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string|NULL|\Nette\DI\Statement $basePath |
28
|
|
|
* @param array $baseUrls |
29
|
|
|
* @param \Nette\DI\Statement $versionStrategy |
30
|
|
|
* |
31
|
|
|
* @return \Nette\DI\Statement |
32
|
|
|
*/ |
33
|
1 |
|
public function createPackageStatement(string $name, $basePath, array $baseUrls, Nette\DI\Statement $versionStrategy): Nette\DI\Statement |
34
|
|
|
{ |
35
|
1 |
|
if (!empty($basePath) && !empty($baseUrls)) { |
36
|
|
|
throw new \LogicException('An asset package cannot have base URLs and base paths.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
if (empty($baseUrls)) { |
40
|
1 |
|
$reference = $this->getPackageDependencyReference( |
41
|
1 |
|
new Nette\DI\Statement(Symfony\Component\Asset\PathPackage::class, [ |
|
|
|
|
42
|
1 |
|
'basePath' => $basePath ?? '', |
43
|
1 |
|
'versionStrategy' => $versionStrategy, |
44
|
|
|
]), |
45
|
1 |
|
$name |
46
|
|
|
); |
47
|
|
|
} else { |
48
|
1 |
|
$reference = $this->getPackageDependencyReference( |
49
|
1 |
|
new Nette\DI\Statement(Symfony\Component\Asset\UrlPackage::class, [ |
|
|
|
|
50
|
1 |
|
'baseUrls' => $baseUrls, |
51
|
1 |
|
'versionStrategy' => $versionStrategy, |
52
|
|
|
]), |
53
|
1 |
|
$name |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return new Nette\DI\Statement($reference); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|\Nette\DI\Statement $definition |
62
|
|
|
* @param string $packageName |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
1 |
|
public function getPackageDependencyReference($definition, string $packageName): string |
67
|
|
|
{ |
68
|
1 |
|
return $this->referenceFacade->getDependencyReference( |
69
|
1 |
|
$definition, |
70
|
1 |
|
'package.' . $packageName, |
71
|
1 |
|
Symfony\Component\Asset\PackageInterface::class |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.