1 | <?php |
||
26 | class Application extends BaseApplication |
||
|
|||
27 | { |
||
28 | /** |
||
29 | * @var Bowerphp |
||
30 | */ |
||
31 | protected $bowerphp; |
||
32 | |||
33 | private static $logo = ' ____ __ |
||
34 | / __ )____ _ _____ _________ / /_ ____ |
||
35 | / __ / __ \ | /| / / _ \/ ___/ __ \/ __ \/ __ \ |
||
36 | / /_/ / /_/ / |/ |/ / __/ / / /_/ / / / / /_/ / |
||
37 | /_____/\____/|__/|__/\___/_/ / .___/_/ /_/ .___/ |
||
38 | /_/ /_/ |
||
39 | '; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | */ |
||
44 | public function __construct() |
||
45 | { |
||
46 | ErrorHandler::register(); |
||
47 | parent::__construct('Bowerphp', '0.3 Powered by BeeLab (bee-lab.net)'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function doRun(InputInterface $input, OutputInterface $output) |
||
54 | { |
||
55 | if (version_compare(PHP_VERSION, '5.3.2', '<')) { |
||
56 | $output->writeln('<warning>Bowerphp only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP ' . PHP_VERSION . ', upgrading is strongly recommended.</warning>'); |
||
57 | } |
||
58 | |||
59 | if ($input->hasParameterOption('--profile')) { |
||
60 | $startTime = microtime(true); |
||
61 | } |
||
62 | |||
63 | if ($newWorkDir = $this->getNewWorkingDir($input)) { |
||
64 | $oldWorkingDir = getcwd(); |
||
65 | chdir($newWorkDir); |
||
66 | } |
||
67 | |||
68 | $result = $this->symfonyDoRun($input, $output); |
||
69 | |||
70 | if (isset($oldWorkingDir)) { |
||
71 | chdir($oldWorkingDir); |
||
72 | } |
||
73 | |||
74 | if (isset($startTime)) { |
||
75 | $output->writeln('<info>Memory usage: ' . round(memory_get_usage() / 1024 / 1024, 2) . 'MB (peak: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'MB), time: ' . round(microtime(true) - $startTime, 2) . 's'); |
||
76 | } |
||
77 | |||
78 | return $result; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function getHelp() |
||
85 | { |
||
86 | return self::$logo . parent::getHelp(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | protected function getDefaultCommands() |
||
93 | { |
||
94 | return array( |
||
95 | new Command\HelpCommand(), |
||
96 | new Command\CommandListCommand(), |
||
97 | new Command\HomeCommand(), |
||
98 | new Command\InfoCommand(), |
||
99 | new Command\InitCommand(), |
||
100 | new Command\InstallCommand(), |
||
101 | new Command\ListCommand(), |
||
102 | new Command\LookupCommand(), |
||
103 | new Command\SearchCommand(), |
||
104 | new Command\UpdateCommand(), |
||
105 | new Command\UninstallCommand(), |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | protected function getDefaultInputDefinition() |
||
113 | { |
||
114 | $definition = parent::getDefaultInputDefinition(); |
||
115 | $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information')); |
||
116 | $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.')); |
||
117 | |||
118 | return $definition; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | protected function getDefaultHelperSet() |
||
125 | { |
||
126 | $helperSet = parent::getDefaultHelperSet(); |
||
127 | if (class_exists('Symfony\Component\Console\Helper\DialogHelper')) { |
||
128 | $helperSet->set(new \Bowerphp\Command\Helper\DialogHelper()); |
||
129 | } else { |
||
130 | $helperSet->set(new \Bowerphp\Command\Helper\QuestionHelper()); |
||
131 | } |
||
132 | |||
133 | return $helperSet; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param InputInterface $input |
||
138 | * @throws \RuntimeException |
||
139 | */ |
||
140 | private function getNewWorkingDir(InputInterface $input) |
||
141 | { |
||
142 | $workingDir = $input->getParameterOption(array('--working-dir', '-d')); |
||
143 | if (false !== $workingDir && !is_dir($workingDir)) { |
||
144 | throw new \RuntimeException('Invalid working directory specified.'); |
||
145 | } |
||
146 | |||
147 | return $workingDir; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Copy of original Symfony doRun, to allow a default command |
||
152 | * |
||
153 | * @param InputInterface $input An Input instance |
||
154 | * @param OutputInterface $output An Output instance |
||
155 | * @param string $default Default command to execute |
||
156 | * |
||
157 | * @return int 0 if everything went fine, or an error code |
||
158 | * @codeCoverageIgnore |
||
159 | */ |
||
160 | private function symfonyDoRun(InputInterface $input, OutputInterface $output, $default = 'list-commands') |
||
193 | } |
||
194 |