1
|
|
|
<?php |
2
|
|
|
namespace Mouf\Html\Utils\WebLibraryManager; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class is a helper class used to easily develop install process for libraries using WebLibraryManager. |
7
|
|
|
* <p>It provides a single static method: installLibrary, that can be used in a Mouf install process |
8
|
|
|
* to install the library and bind it to the default webLibraryManager.</p> |
9
|
|
|
* |
10
|
|
|
* @author David Négrier |
11
|
|
|
*/ |
12
|
|
|
use Mouf\MoufManager; |
13
|
|
|
|
14
|
|
|
class WebLibraryInstaller { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @param string $instanceName The instance name to create |
19
|
|
|
* @param array<string> $jsFiles The list of js files to add (relative to root directory) |
20
|
|
|
* @param array<string> $cssFiles The list of css files to add (relative to root directory) |
21
|
|
|
* @param array<string> $dependencies The list of dependencies (the name of the instances) |
22
|
|
|
* @param bool $bindToWebLibraryManager Whether we should bind the declared weblibrary directly in the defaultWebLibraryManager or not. |
23
|
|
|
* @param MoufManager $moufManager The moufManager to be used for installation (defaults to default mouf manager) |
24
|
|
|
*/ |
25
|
|
|
public static function installLibrary($instanceName, array $jsFiles, array $cssFiles = array(), array $dependencies = array(), $bindToWebLibraryManager = true, MoufManager $moufManager = null) { |
26
|
|
|
if ($moufManager === null) { |
27
|
|
|
$moufManager = MoufManager::getMoufManager(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($moufManager->instanceExists($instanceName)) { |
31
|
|
|
$library = $moufManager->getInstanceDescriptor($instanceName); |
32
|
|
|
} else { |
33
|
|
|
$library = $moufManager->createInstance("Mouf\\Html\\Utils\\WebLibraryManager\\WebLibrary"); |
34
|
|
|
$library->setName($instanceName); |
35
|
|
|
} |
36
|
|
|
$library->getProperty("jsFiles")->setValue($jsFiles); |
37
|
|
|
$library->getProperty("cssFiles")->setValue($cssFiles); |
38
|
|
|
|
39
|
|
|
$dependenciesInstances = array(); |
40
|
|
|
foreach ($dependencies as $dependencyName) { |
41
|
|
|
$dependenciesInstances[] = $moufManager->getInstanceDescriptor($dependencyName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$library->getProperty("dependencies")->setValue($dependenciesInstances); |
45
|
|
|
|
46
|
|
|
if ($bindToWebLibraryManager) { |
47
|
|
|
$webLibraryManager = $moufManager->getInstanceDescriptor('defaultWebLibraryManager'); |
48
|
|
|
if ($webLibraryManager) { |
49
|
|
|
$libraries = $webLibraryManager->getSetterProperty("webLibraries")->getValue(); |
50
|
|
|
if (array_search($library, $libraries) === false) { |
51
|
|
|
$libraries[] = $library; |
52
|
|
|
$webLibraryManager->getSetterProperty("webLibraries")->setValue($libraries); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|