-
Notifications
You must be signed in to change notification settings - Fork 88
/
ScriptHandler.php
107 lines (85 loc) · 2.94 KB
/
ScriptHandler.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Fuz\Symfony\Collection;
use Composer\Script\Event;
class ScriptHandler
{
public static function postInstall(Event $event)
{
self::handle($event);
}
public static function postUpdate(Event $event)
{
self::handle($event);
}
protected static function handle(Event $event)
{
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
self::installTwigFormTheme($vendorDir);
self::installJQueryPlugin($vendorDir);
}
/**
* @param $dirs
* @param bool $writableCheck
* @return string|null
*/
protected static function searchDirectory($dirs, $writableCheck = true)
{
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
echo "Directory '{$dir}' doesn't exist.\n";
continue;
}
if ($writableCheck && !is_writable($dir)) {
echo "Directory '{$dir}' is not writeable.\n";
continue;
}
return $dir;
}
return null;
}
protected static function installTwigFormTheme($vendorDir)
{
$collectionDir = __DIR__;
echo "*** ninsuo/symfony-collection: Installing Twig form theme... \t";
$viewsDirs = [
$vendorDir . '/../app/Resources/views', //symfony < 3.4
$vendorDir . '/../templates', //symfony >= 3.4 new stucture
];
$viewCopyDir = self::searchDirectory($viewsDirs);
if ($viewCopyDir === null) {
echo 'FAILED: with directories ' . \implode(',', $viewsDirs);
return;
}
copy($collectionDir . '/jquery.collection.html.twig', $viewCopyDir . '/jquery.collection.html.twig');
echo 'SUCCESS: ';
echo "You can now use {% form_theme form 'jquery.collection.html.twig' %}\n";
}
protected static function installJQueryPlugin($vendorDir)
{
$collectionDir = __DIR__;
echo "*** ninsuo/symfony-collection: Installing jQuery plugin... \t";
$webDirs = [
$vendorDir . '/../web', //symfony < 3.4
$vendorDir . '/../public', //symfony >= 3.4 new stucture
];
$webDir = self::searchDirectory($webDirs, false);
if ($webDir === null) {
echo 'FAILED: with directories ' . \implode(',', $webDirs);
return;
}
$jsDir = $webDir . '/js';
if (!is_dir($jsDir) && !mkdir($jsDir)) {
echo 'FAILED: ';
echo "Directory '$jsDir' can't be created.\n";
return;
}
if (!is_writable($jsDir)) {
echo 'FAILED: ';
echo "Directory '$jsDir' is not writeable.\n";
return;
}
copy($collectionDir . '/jquery.collection.js', $jsDir . '/jquery.collection.js');
echo 'SUCCESS: ';
echo 'You can now use <script type="text/javascript" src={{ asset(\'js/jquery.collection.js\') }} />' . "\n";
}
}