1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Selim\Commands; |
4
|
|
|
|
5
|
|
|
use File_Iterator_Factory; |
6
|
|
|
use Selim\Util; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\Question; |
12
|
|
|
|
13
|
|
|
class FindSitesCommand extends SelimCommand{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
parent::configure(); |
17
|
|
|
$this |
18
|
|
|
->setName('find') |
19
|
|
|
->setDescription('Find silverstripe sites on your hdd') |
20
|
|
|
->addArgument( |
21
|
|
|
'path', |
22
|
|
|
InputArgument::REQUIRED, |
23
|
|
|
'Path where the script should start searching recursively' |
24
|
|
|
)->addOption( |
25
|
|
|
'skip-known-paths', |
26
|
|
|
's', |
27
|
|
|
InputOption::VALUE_NONE, |
28
|
|
|
'Skip all paths which are already in your selim config' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
$cfg = $this->getSelimConfig($input); |
35
|
|
|
$skippaths = $input->getOption("skip-known-paths") ? true : false; |
36
|
|
|
$path = realpath($input->getArgument("path")); |
37
|
|
|
$projects = array(); |
38
|
|
|
$question_helper = $this->getHelper("question"); |
39
|
|
|
|
40
|
|
|
$output->write("Start searching _config.php files..."); |
41
|
|
|
$fact = new File_Iterator_Factory(); |
42
|
|
|
$iterator = $fact->getFileIterator($path,"php","_config"); |
43
|
|
|
$output->writeln("OK"); |
44
|
|
|
|
45
|
|
|
$output->write("Filter for project paths..."); |
46
|
|
|
while($file = $iterator->current()){ |
47
|
|
|
$content = Util::stripPhpComments(file_get_contents($file)); |
48
|
|
|
if(preg_match("/\\\$project\\s=/",$content)){ |
49
|
|
|
array_push($projects,dirname($file)); |
50
|
|
|
} |
51
|
|
|
$iterator->next(); |
52
|
|
|
} |
53
|
|
|
$output->writeln("OK"); |
54
|
|
|
|
55
|
|
|
if(count($projects)){ |
56
|
|
|
$sites_added = false; |
57
|
|
|
$output->writeln("found ".count($projects)." possible sites"); |
58
|
|
|
foreach($projects as $p){ |
59
|
|
|
if($skippaths && $cfg->sitePathExists($p)) continue; |
60
|
|
|
|
61
|
|
|
$question = new Question("Please enter name for '$p' (leave empty to skip)"); |
62
|
|
|
do { |
63
|
|
|
$name = $question_helper->ask($input, $output, $question); |
64
|
|
|
}while($cfg->siteExists($name)); |
65
|
|
|
|
66
|
|
|
if($name){ |
67
|
|
|
$sites_added = true; |
68
|
|
|
$cfg->addSite($name,$p); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
if($sites_added) { |
72
|
|
|
$output->write("Writing config.json ..."); |
73
|
|
|
$cfg->write(); |
74
|
|
|
$output->writeln("OK"); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |