-
Notifications
You must be signed in to change notification settings - Fork 38
/
modman.php
1238 lines (1114 loc) · 39.3 KB
/
modman.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class Modman {
/**
* the .modman directory is missing
*/
const ERR_NOT_INITIALIZED = 1;
/**
* no modman file in the linked directory
*/
const ERR_NO_MODMAN_FILE = 2;
/**
* runs and dispatches the modman program
*
* @param $aParameters - this is just a representation of $argv (the parameters which were used to launch the program)
*/
public function run($aParameters) {
try {
if (!isset($aParameters[1])) {
// show help if called without parameters
$this->printHelp();
exit;
}
$bForce = in_array('--force', $aParameters, true);
$bCopy = in_array('--copy', $aParameters, true);
switch ($aParameters[1]) {
case 'link':
if (!isset($aParameters[2])) {
throw new Exception('please specify target directory');
}
$sLinkPath = realpath($aParameters[2]);
if (!$sLinkPath){
throw new Exception('Link path is invalid!');
}
$oLink = new Modman_Command_Link($sLinkPath);
$oLink->createSymlinks($bForce);
echo 'Successfully create symlink new module \'' . basename($sLinkPath) . '\'' . PHP_EOL;
break;
case 'init':
$sCwd = getcwd();
$sBaseDir = null;
if (isset($aParameters[2])) {
$sBaseDir = $aParameters[2];
}
$sInitPath = realpath($sCwd);
$oInit = new Modman_Command_Init();
$oInit->doInit($sInitPath, $sBaseDir);
break;
case 'deploy':
if (!isset($aParameters[2])) {
throw new Exception('please specify module name');
}
$oDeploy = new Modman_Command_Deploy($aParameters[2]);
$oDeploy->doDeploy($bForce, $bCopy);
echo $aParameters[2] . ' has been deployed under ' . getcwd() . PHP_EOL;
break;
case 'repair':
$bForce = true;
case 'deploy-all':
$oDeployAll = new Modman_Command_All('Modman_Command_Deploy');
$oDeployAll->doDeploy($bForce, $bCopy);
break;
case 'clean':
$oClean = new Modman_Command_Clean();
$oClean->doClean();
break;
case 'remove':
if (!isset($aParameters[2])) {
throw new Exception('please specify module name');
}
$oRemove = new Modman_Command_Remove($aParameters[2]);
$oRemove->doRemove($bForce);
break;
case 'create':
$oCreate = new Modman_Command_Create();
$iIncludeOffset = array_search('--include', $aParameters);
$bListHidden = array_search('--include-hidden', $aParameters);
if ($iIncludeOffset){
$oCreate->setIncludeFile($aParameters[$iIncludeOffset + 1]);
}
$oCreate->doCreate($bForce, $bListHidden);
break;
case 'clone':
if (!isset($aParameters[2])){
throw new Exception('Please specify git repository URL');
}
$bCreateModman = array_search('--create-modman', $aParameters);
$oClone = new Modman_Command_Clone($aParameters[2], new Modman_Command_Create());
$oClone->doClone($bForce, $bCreateModman);
break;
default:
throw new Exception('command does not exist');
}
} catch (Exception $oException) {
// set small timeout, no big delays for a funny feature
$rCtx = stream_context_create(array('http'=>
array(
'timeout' => 1,
)
));
$sMessage = $oException->getMessage();
$sCowsay = @file_get_contents('http://cowsay.morecode.org/say?message=' . urlencode($sMessage) . '&format=text', false, $rCtx);
if ($sCowsay) {
echo $sCowsay;
} else {
echo '-----' . PHP_EOL;
echo 'An error occured:' . PHP_EOL;
echo $sMessage . PHP_EOL;
echo '-----';
}
echo PHP_EOL . PHP_EOL;
$this->printHelp();
}
}
/**
* prints the help
*/
public function printHelp(){
$sHelp = <<< EOH
PHP-based module manager, originally implemented as bash-script
(for original implementation see https://github.com/colinmollenhour/modman)
Following general commands are currently supported:
- link (optional --force)
- init (optional <basedir>)
- repair
- deploy (optional --force)
- deploy-all (optional --force)
- clean
- create (optional --force, --include <include_file> and --include-hidden)
- clone (optional --force, --create-modman)
Currently supported in modman-files:
- symlinks (with wildcards)
- @import and @shell command
EOH;
echo $sHelp . PHP_EOL;
}
}
class Modman_Command_All {
private $sClassName;
/**
* constructor for a command
*
* @param string $sClassName
*/
public function __construct($sClassName) {
$this->sClassName = $sClassName;
}
/**
* returns all linked modules
*
* @return array
* @throws Exception if modman directory does not exist
*/
private function getAllModules() {
if (!file_exists(Modman_Command_Init::MODMAN_DIRECTORY_NAME)) {
throw new Exception ('No modman directory found. You need to call "modman init" to create it.' . PHP_EOL
. 'Please consider the documentation below.', Modman::ERR_NOT_INITIALIZED);
}
$aDirEntries = scandir(Modman_Command_Init::MODMAN_DIRECTORY_NAME);
unset($aDirEntries[array_search('.', $aDirEntries)]);
unset($aDirEntries[array_search('..', $aDirEntries)]);
$iBaseDir = array_search(Modman_Command_Init::getBaseDirFile(), $aDirEntries);
if ($iBaseDir !== false) {
unset($aDirEntries[$iBaseDir]);
}
return $aDirEntries;
}
/**
* calls a method on all modules
*
* @param string $sMethodName the method name to call
* @param array $aArguments the parameters to give that method
*/
public function __call($sMethodName, $aArguments) {
foreach ($this->getAllModules() as $sModuleName) {
$oClass = new $this->sClassName($sModuleName);
call_user_func_array(array($oClass, $sMethodName), $aArguments);
}
}
}
class Modman_Command_Init {
// directory name
const MODMAN_DIRECTORY_NAME = '.modman';
const MODMAN_BASEDIR_FILE = '.basedir';
public static function getBaseDirFile() {
return self::MODMAN_DIRECTORY_NAME . DIRECTORY_SEPARATOR . self::MODMAN_BASEDIR_FILE;
}
/**
* Creates directory ".modman" if it doesn't exist
*
* @param string
* @param string
*/
public function doInit($sDirectory, $sBaseDir = null) {
$sModmanDirectory = $sDirectory . DIRECTORY_SEPARATOR . self::MODMAN_DIRECTORY_NAME;
if (!is_dir($sModmanDirectory)){
mkdir($sModmanDirectory);
}
if (!is_null($sBaseDir)) {
file_put_contents(self::getBaseDirFile(), $sBaseDir);
}
}
}
class Modman_Command_Link {
private $sTarget;
/**
* constructor
*
* @param string $sTarget target to link
*/
public function __construct($sTarget) {
if (empty($sTarget)) {
throw new Exception('no source defined');
}
$this->sTarget = $sTarget;
}
/**
* creates the symlinks
*
* @param bool $bForce if true errors will be ignored
* @throws Exception if module is already linked
*/
public function createSymlinks($bForce = false) {
$sModuleName = basename($this->sTarget);
$sModuleSymlink = Modman_Command_Init::MODMAN_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $sModuleName;
if (is_link($sModuleSymlink)) {
throw new Exception($sModuleName . ' is already linked');
}
symlink($this->sTarget, $sModuleSymlink);
$oDeploy = new Modman_Command_Deploy($sModuleName);
$oDeploy->doDeploy($bForce);
}
}
class Modman_Command_Link_Line {
private $sTarget, $sSymlink;
/**
* constructor
*
* @param array $aDirectories - key 0 = source; key 1 = target
*/
public function __construct($aDirectories) {
$this->sTarget = $aDirectories[0];
if (empty($aDirectories[1])) {
$this->sSymlink = $this->sTarget;
} else {
$this->sSymlink = $aDirectories[1];
}
}
/**
* returns the target
*
* @return string
*/
public function getTarget() {
return $this->rtrimDS($this->sTarget);
}
/**
* returns the symlink
*
* @return string
*/
public function getSymlink() {
$sBaseDir = getcwd();
$sBaseDirFile = Modman_Command_Init::getBaseDirFile();
if (file_exists($sBaseDirFile)) {
$sBaseDir = rtrim(array_shift(file($sBaseDirFile, FILE_IGNORE_NEW_LINES)));
}
return $sBaseDir . DIRECTORY_SEPARATOR . $this->rtrimDS($this->sSymlink);
}
/**
* fixes trailing slashes on *nix systems
*
* @param $sDir
* @return string
*/
private function rtrimDS($sDir) {
return rtrim($sDir, DIRECTORY_SEPARATOR);
}
/**
* returns the symlink base dir
*
* @return string
*/
public function getSymlinkBaseDir() {
return dirname($this->getSymlink());
}
}
class Modman_Reader {
const MODMAN_FILE_NAME = 'modman';
private $aFileContent = array();
private $aObjects = array();
private $sClassName;
private $aShells = array();
private $sModuleDirectory;
/**
* constructor
*
* @param string $sDirectory - where to read
*/
public function __construct($sDirectory) {
$this->sModuleDirectory = $sDirectory;
$this->aFileContent = file($sDirectory . DIRECTORY_SEPARATOR . self::MODMAN_FILE_NAME);
$sFileName = $sDirectory . DIRECTORY_SEPARATOR . self::MODMAN_FILE_NAME;
if (!file_exists($sFileName)) {
throw new Exception ('The directory you would like to link has no modman file.' . PHP_EOL
. 'Cannot link to this directory.', Modman::ERR_NO_MODMAN_FILE);
}
$this->aFileContent = file($sFileName);
}
/**
* returns the params of a line as array
*
* @param string $sRow line separated by spaces
* @return array
*/
private function getParamsArray($sRow){
return explode(' ', preg_replace('/\s+/', ' ', $sRow));
}
/**
* returns an array of objects per row
*
* @param string $sClassName class which should be used to initialize each row
* @return array
*/
public function getObjectsPerRow($sClassName) {
$this->sClassName = $sClassName;
foreach ($this->aFileContent as $sLine) {
if (substr($sLine, 0, 1) == '#') {
// skip comments
continue;
}
$aParameters = $this->getParamsArray($sLine);
if (substr($sLine, 0, 7) == '@import') {
$this->doImport($aParameters);
continue;
} elseif (substr($sLine, 0, 6) == '@shell') {
unset($aParameters[0]);
$this->aShells[] = implode(' ', $aParameters);
continue;
} elseif (substr($sLine, 0, 1) == '@'){
echo 'Do not understand: ' . $sLine . PHP_EOL;
continue;
}
if (strstr($sLine, '*')) {
foreach (glob($this->sModuleDirectory . DIRECTORY_SEPARATOR . $aParameters[0]) as $sFilename) {
$sRelativeFilename = substr($sFilename, strlen($this->sModuleDirectory . DIRECTORY_SEPARATOR));
$sRelativeTarget = str_replace(str_replace('*', '', $aParameters[0]), $aParameters[1], $sRelativeFilename);
$this->aObjects[] = new $sClassName(array($sRelativeFilename, $sRelativeTarget));
}
} else {
$this->aObjects[] = new $sClassName($aParameters);
}
}
return $this->aObjects;
}
/**
* imports another file
*
* @param array $aCommandParams params submitted to import
* @throws Exception if the path could not be parsed
*/
private function doImport($aCommandParams){
$sDirectoryName = realpath($this->sModuleDirectory . DIRECTORY_SEPARATOR . $aCommandParams[1]);
if (!$sDirectoryName){
throw new Exception('The import path could not be parsed!');
}
$oModmanReader = new Modman_Reader($sDirectoryName);
$aObjects = $oModmanReader->getObjectsPerRow($this->sClassName);
// Hack to make paths relative to $this->sModuleDirectory
// Fixes the case when the paths are relative to nested folder, eg "../../file"
$sBaseDir = getcwd();
$aObjectsFixed = array();
foreach ($aObjects as $iLine => $oLine) {
$sTarget = $oLine->getTarget();
$sTarget = realpath($sDirectoryName . DIRECTORY_SEPARATOR . $sTarget);
$sTarget = str_replace($this->sModuleDirectory, '', $sTarget);
$sTarget = trim($sTarget, DIRECTORY_SEPARATOR);
$sSymlink = $oLine->getSymlink();
$sSymlink = str_replace($sBaseDir, '', $sSymlink);
$sSymlink = trim($sSymlink, DIRECTORY_SEPARATOR);
$aObjectsFixed[] = new $this->sClassName(array($sTarget, $sSymlink));
}
$this->aObjects = array_merge($this->aObjects, $aObjectsFixed);
}
/**
* returns all collected shell commands
*
* @return array
*/
public function getShells() {
return $this->aShells;
}
}
class Modman_Reader_Conflicts {
private $aConflicts = array();
/**
* checks for conflicts in file
*
* @param string $sSymlink symlink name
* @param string $sType type (either dir or file)
* @param string $sTarget = false target where the symlink should link to
*/
public function checkForConflict($sSymlink, $sType, $sTarget = false) {
if (is_link($sSymlink)) {
if (
!(
$sType == 'link'
AND realpath($sSymlink) == realpath($sTarget)
)
) {
$this->aConflicts[$sSymlink] = 'link';
}
} elseif (file_exists($sSymlink)) {
if (is_dir($sSymlink)) {
if ($sType == 'dir') {
return;
}
$this->aConflicts[$sSymlink] = 'dir';
} else {
$this->aConflicts[$sSymlink] = 'file';
}
}
}
/**
* returns if there are any conflicts
*
* @return bool true if conflicts exist
*/
public function hasConflicts() {
return (count($this->aConflicts) > 0);
}
/**
* returns conflicts as human readable string
*
* @return string
*/
public function getConflictsString() {
$sString = '';
foreach ($this->aConflicts as $sFilename => $sType) {
switch ($sType) {
case 'dir':
$sString .= $sFilename . ' is an existing directory.' . PHP_EOL;
break;
case 'file':
$sString .= $sFilename . ' is an existing file.' . PHP_EOL;
break;
case 'link':
$sString .= $sFilename . ' is an existing link pointing to ' . realpath($sFilename) . '.' . PHP_EOL;
break;
}
}
return $sString;
}
/**
* removes a linked module
*/
public function cleanup() {
$oResourceRemover = new Modman_Resource_Remover();
foreach ($this->aConflicts as $sFilename => $sType) {
switch ($sType) {
case 'dir':
$oResourceRemover->doRemoveFolderRecursively($sFilename);
break;
case 'file':
case 'link':
$oResourceRemover->doRemoveResource($sFilename);
break;
}
}
}
}
class Modman_Command_Deploy {
private $sModuleName;
/**
* constructor
*
* @param string $sModuleName which module to deploy
* @throws Exception
*/
public function __construct($sModuleName) {
if (empty($sModuleName)) {
throw new Exception('please provide a module name to deploy');
}
$this->sModuleName = $sModuleName;
}
/**
* executes the deploy
*
* @param bool $bForce=false true if errors should be ignored
* @param bool $bCopy=false true if files and folders should be copied instead of symlinked
* @throws Exception on error
*/
public function doDeploy($bForce = false, $bCopy = false) {
if ($this->sModuleName === Modman_Command_Init::MODMAN_BASEDIR_FILE) {
return;
}
$oModmanModuleSymlink = new Modman_Module_Symlink($this->sModuleName);
$sTarget = $oModmanModuleSymlink->getModmanModuleSymlinkPath();
$this->oReader = new Modman_Reader($sTarget);
$aLines = $this->oReader->getObjectsPerRow('Modman_Command_Link_Line');
$oConflicts = new Modman_Reader_Conflicts();
foreach ($aLines as $iLine => $oLine) {
/* @var $oLine Modman_Command_Link_Line */
if ($oLine->getTarget() AND $oLine->getSymlink()) {
$sDirectoryName = $oLine->getSymlinkBaseDir();
if (!is_dir($sDirectoryName)) {
$oConflicts->checkForConflict($sDirectoryName, 'dir');
}
$oConflicts->checkForConflict($oLine->getSymlink(), 'link', $oLine->getTarget());
} else {
unset($aLines[$iLine]);
}
}
if ($oConflicts->hasConflicts()) {
$sConflictsString = 'conflicts detected: ' . PHP_EOL .
$oConflicts->getConflictsString() . PHP_EOL;
if ($bForce) {
echo $sConflictsString;
echo 'Doing cleanup ... ' . PHP_EOL;
$oConflicts->cleanup();
} else {
throw new Exception($sConflictsString .
'use --force'
);
}
}
foreach ($aLines as $oLine) {
/* @var $oLine Modman_Command_Link_Line */
$sFullTarget = $sTarget . DIRECTORY_SEPARATOR . $oLine->getTarget();
if (!file_exists($sFullTarget)) {
throw new Exception('can not link to non-existing file ' . $sFullTarget);
}
// create directories if path does not exist
$sDirectoryName = $oLine->getSymlinkBaseDir();
if (!is_dir($sDirectoryName)) {
echo 'Create directory ' . $sDirectoryName . PHP_EOL;
mkdir($sDirectoryName, 0777, true);
}
if (!is_link($oLine->getSymlink())) {
echo ' Applied: ' . $oLine->getSymlink() . ' ' . $sFullTarget . PHP_EOL;
if ($bCopy) {
if (is_dir($sFullTarget)) {
mkdir($oLine->getSymlink());
$oIterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$sFullTarget,
\RecursiveDirectoryIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($oIterator as $oItem) {
if ($oItem->isDir()) {
mkdir($oLine->getSymlink() . DIRECTORY_SEPARATOR . $oIterator->getSubPathName());
} else {
copy($oItem, $oLine->getSymlink() . DIRECTORY_SEPARATOR . $oIterator->getSubPathName());
}
}
} else {
copy($sFullTarget, $oLine->getSymlink());
}
} else {
symlink(
$sFullTarget,
$oLine->getSymlink()
);
}
}
}
foreach ($this->oReader->getShells() as $sShell) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$sShell = str_replace('rm -rf', 'deltree', $sShell);
}
$sShell = str_replace('$MODULE', $sTarget, $sShell);
$sShell = str_replace('$PROJECT', getcwd(), $sShell);
system($sShell);
}
}
}
class Modman_Module_Symlink {
private $sModuleName;
/**
* constructor
*
* @param string $sModuleName module name
* @throws Exception
*/
public function __construct($sModuleName){
if (empty($sModuleName)) {
throw new Exception('please provide a module name to deploy');
}
$this->sModuleName = $sModuleName;
}
/**
* returns module symlink
*
* @return string
*/
public function getModmanModuleSymlink(){
$sModmanModuleSymlink = Modman_Command_Init::MODMAN_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $this->sModuleName;
return $sModmanModuleSymlink;
}
/**
* returns module symlink path
*
* @return string
* @throws Exception if symlink is not linked
*/
public function getModmanModuleSymlinkPath(){
$sModmanModuleSymlink = $this->getModmanModuleSymlink();
if (!is_link($sModmanModuleSymlink) AND !is_dir($sModmanModuleSymlink)) {
throw new Exception($this->sModuleName . ' is not initialized, please clone or link it');
}
$sTarget = realpath($sModmanModuleSymlink);
return $sTarget;
}
}
class Modman_Command_Clean {
private $aDeadSymlinks = array();
/**
* executes the clean command
*/
public function doClean() {
$oResourceRemover = new Modman_Resource_Remover();
foreach ($this->getDeadSymlinks() as $sSymlink) {
echo 'Remove ' . $sSymlink . '.' . PHP_EOL;
$oResourceRemover->doRemoveResource($sSymlink);
}
}
/**
* returns dead symlinks
*
* @param string $sDirectory=NULL define directory to work on, if not defined uses getcwd()
* @return array list of dead symlinks
*/
private function getDeadSymlinks($sDirectory = NULL) {
if (is_null($sDirectory)) {
$sDirectory = getcwd();
}
$this->scanForDeadSymlinks($sDirectory);
return $this->aDeadSymlinks;
}
/**
* recursive scan for dead symlinks
*
* @param string $sDirectory
*/
private function scanForDeadSymlinks($sDirectory) {
foreach (scandir($sDirectory) as $sFilename) {
if ($sFilename == '.' OR $sFilename == '..') {
continue;
}
$sFullFilename = $sDirectory . DIRECTORY_SEPARATOR . $sFilename;
if (is_dir($sFullFilename) AND !is_link($sFullFilename)) {
$this->scanForDeadSymlinks($sFullFilename);
} elseif (is_link($sFullFilename) AND !file_exists(realpath($sFullFilename))) {
$this->aDeadSymlinks[] = $sFullFilename;
}
}
}
}
class Modman_Command_Remove {
/**
* constructor
*
* @param string $sModuleName define module name
* @throws Exception
*/
public function __construct($sModuleName) {
if (empty($sModuleName)) {
throw new Exception('please provide a module name to deploy');
}
$this->sModuleName = $sModuleName;
}
/**
* executres remove
*
* @param bool $bForce = false, true ignores errors
* @throws Exception on error
*/
public function doRemove($bForce = false){
$oModmanModuleSymlink = new Modman_Module_Symlink($this->sModuleName);
$sTarget = $oModmanModuleSymlink->getModmanModuleSymlinkPath();
$this->oReader = new Modman_Reader($sTarget);
$aLines = $this->oReader->getObjectsPerRow('Modman_Command_Link_Line');
$oResourceRemover = new Modman_Resource_Remover();
foreach ($aLines as $oLine) {
$sOriginalPath = $oLine->getTarget();
$sSymlinkPath = $oLine->getSymlink();
if (is_link($sSymlinkPath)
AND file_exists($sTarget . DIRECTORY_SEPARATOR . $sOriginalPath)){
if (is_link($sSymlinkPath)){
$oResourceRemover->doRemoveResource($sSymlinkPath);
} elseif ($bForce){
$oResourceRemover->doRemoveResource($sSymlinkPath);
} else {
throw new Exception('Problem with removing ' . $sSymlinkPath . ' - use --force');
}
}
}
$oResourceRemover->doRemoveResource($oModmanModuleSymlink->getModmanModuleSymlink());
}
}
class Modman_Command_Create {
private $aLinks = array();
private $sIncludeFilePath;
private $bListHidden = false;
const MAGENTO_MODULE_CODE_RELATIVE_PATH_DEPTH = 4;
const MAGENTO_MODULE_DESIGN_RELATIVE_PATH_DEPTH = 7;
/**
* sets the include file
*
* @param string $sFilename
* @throws Exception if $sFilename does not exist
*/
public function setIncludeFile($sFilename){
$sFilePath = realpath($sFilename);
if (!$sFilePath){
throw new Exception("please provide a valid include file");
} else {
$this->sIncludeFilePath = $sFilePath;
}
}
/**
* checks if a directory is empty
*
* @param string $sDirectoryPath
* @return bool true if directory is empty (broken symlinks count as empty)
*/
private function isDirectoryEmpty($sDirectoryPath){
if (false === @readlink($sDirectoryPath)) {
return true;
}
$aCurrentDirectoryListing = scandir($sDirectoryPath);
return count($aCurrentDirectoryListing) <= 2;
}
/**
* checks if node is a hidden once
*
* @param string $sNode
* @return bool true for hidden files
*/
private function isHiddenNode($sNode){
return strlen($sNode) > 2 AND substr($sNode, 0, 1) == '.';
}
/**
* checks if directory is a magento module
*
* @param string $sDirectoryPathToCheck
* @return bool true if directory is a magento module
*/
private function isMagentoModuleDirectory($sDirectoryPathToCheck){
$aPathParts = explode(DIRECTORY_SEPARATOR, $sDirectoryPathToCheck);
$iAppPosition = array_search('app', $aPathParts);
if (!$iAppPosition){
return false;
}
return (
$this->isMagentoModuleCodeDirectory($aPathParts, $iAppPosition)
OR $this->isMagentoModuleDesignDirectory($aPathParts, $iAppPosition)
);
}
/**
* checks if directory is the magento code directory
*
* @param array $aPathParts - all path parts from this directory
* @param integer $iAppPosition - position of app directory in $aPathParts
* @return bool true if directory is the magento code directory
*/
private function isMagentoModuleCodeDirectory($aPathParts, $iAppPosition) {
if (!isset($aPathParts[$iAppPosition + self::MAGENTO_MODULE_CODE_RELATIVE_PATH_DEPTH])){
return false;
}
if ($aPathParts[$iAppPosition + 1] == 'code'
AND in_array($aPathParts[$iAppPosition + 2], array('community', 'local'))){
return true;
}
}
/**
* checks if directory is the magento design directory
*
* @param array $aPathParts - all path parts from this directory
* @param integer $iAppPosition - position of app directory in $aPathParts
* @return bool true if directory is the magento design directory
*/
private function isMagentoModuleDesignDirectory($aPathParts, $iAppPosition) {
if (!isset($aPathParts[$iAppPosition + self::MAGENTO_MODULE_DESIGN_RELATIVE_PATH_DEPTH])){
return false;
}
if ($aPathParts[$iAppPosition + 1] == 'design'
AND (
$aPathParts[$iAppPosition + 2] == 'frontend'
OR $aPathParts[$iAppPosition + 2] == 'adminhtml'
OR $aPathParts[$iAppPosition + 2] == 'install'
)
AND $aPathParts[$iAppPosition + 3] == 'base'
AND $aPathParts[$iAppPosition + 4] == 'default'
AND $aPathParts[$iAppPosition + 5] == 'template'
){
return true;
}
}
/**
* returns directory structure
*
* @param string $sDirectoryPath
* @return array with directory structure
*/
private function getDirectoryStructure($sDirectoryPath) {
$aResult = array();
$aCurrentDirectoryListing = scandir($sDirectoryPath);
foreach ($aCurrentDirectoryListing as $sNode){
$sDirectoryPathToCheck = $sDirectoryPath . DIRECTORY_SEPARATOR . $sNode;
if ((!$this->isHiddenNode($sNode) OR $this->bListHidden)
AND !in_array($sNode, array('.', '..', 'modman', 'README', 'README.md', 'composer.json', 'atlassian-ide-plugin.xml'))){
if (is_dir($sDirectoryPathToCheck)
AND !$this->isDirectoryEmpty($sDirectoryPathToCheck)
AND !$this->isMagentoModuleDirectory($sDirectoryPathToCheck)){
$aResult[$sNode] = $this->getDirectoryStructure($sDirectoryPathToCheck);
} else {
$aResult[] = $sNode;
}
}
}
return $aResult;
}
/**
* generates link list from directory structure
*
* @param array $aDirectoryStructure created by $this->getDirectoryStructure(string)
* @param array $aPathElements = array()
*/
private function generateLinkListFromDirectoryStructure($aDirectoryStructure, $aPathElements = array()){
foreach ($aDirectoryStructure as $sDirectory => $mElements){
if (!is_array($mElements)){
$this->aLinks[] =
(count($aPathElements) > 0 ? implode(DIRECTORY_SEPARATOR, $aPathElements) . DIRECTORY_SEPARATOR : '') .
$mElements;
} else {
$this->generateLinkListFromDirectoryStructure($mElements, array_merge($aPathElements, array($sDirectory)));
}
}
}
/**
* returns modman file path
*
* @return string
*/
private function getModmanFilePath(){
return getcwd() . DIRECTORY_SEPARATOR . Modman_Reader::MODMAN_FILE_NAME;
}
/**
* checks if modman file exists
*
* @return bool true if modman file exists
*/
private function existsModmanFile(){
return file_exists($this->getModmanFilePath());
}
/**
* generates modman file
*/
private function generateModmanFile(){
if (file_exists($this->getModmanFilePath())){
unlink($this->getModmanFilePath());
}
$sOutput = '';
foreach ($this->aLinks as $sLink){
$sLink = '/' . $sLink;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$sLink = str_replace('\\', '/', $sLink);
}
$sOutput .= $sLink . ' ' . $sLink . PHP_EOL;
}
$rModmanFile = fopen($this->getModmanFilePath(), 'w');
fputs($rModmanFile,$sOutput);
// if include file defined, include it to the modman
if ($this->sIncludeFilePath){
$sIncludeFileContent = file_get_contents($this->sIncludeFilePath);
fputs($rModmanFile, "\n" . $sIncludeFileContent);
}
fclose($rModmanFile);
}
/**
* executes create command
*
* @param bool $bForce - if true errors will be ignored
* @param bool $bListHidden = false, if true hidden files will be listed
* @throws Exception on errors
*/
public function doCreate($bForce, $bListHidden = false){
$this->bListHidden = $bListHidden;
$aDirectoryStructure = $this->getDirectoryStructure(getcwd());
$this->generateLinkListFromDirectoryStructure($aDirectoryStructure);
if ($this->existsModmanFile() AND !$bForce){
throw new Exception('modman file ' . $this->getModmanFilePath() . ' already exists. Use --force');
} else {
$this->generateModmanFile();
}
}
}
class Modman_Command_Clone {