|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony Installer package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Installer; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | +use Symfony\Component\Filesystem\Filesystem; |
| 17 | + |
| 18 | +/** |
| 19 | + * This command creates a full-featured Symfony demo application. |
| 20 | + * |
| 21 | + * @author Javier Eguiluz <javier.eguiluz@gmail.com> |
| 22 | + */ |
| 23 | +class DemoCommand extends DownloadCommand |
| 24 | +{ |
| 25 | + /** @var Filesystem */ |
| 26 | + protected $fs; |
| 27 | + protected $projectName; |
| 28 | + protected $projectDir; |
| 29 | + protected $remoteFileUrl; |
| 30 | + protected $downloadedFilePath; |
| 31 | + protected $requirementsErrors = array(); |
| 32 | + /** @var OutputInterface */ |
| 33 | + protected $output; |
| 34 | + |
| 35 | + protected function configure() |
| 36 | + { |
| 37 | + $this |
| 38 | + ->setName('demo') |
| 39 | + ->setDescription('Creates a demo Symfony project.') |
| 40 | + ; |
| 41 | + } |
| 42 | + |
| 43 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 44 | + { |
| 45 | + $this->remoteFileUrl = 'http://symfony.com/download?v=Symfony_Demo'; |
| 46 | + $this->output = $output; |
| 47 | + $this->fs = new Filesystem(); |
| 48 | + $this->projectDir = getcwd(); |
| 49 | + |
| 50 | + $i = 1; |
| 51 | + $projectName = 'symfony_demo'; |
| 52 | + while (file_exists($this->projectDir.DIRECTORY_SEPARATOR.$projectName)) { |
| 53 | + $projectName = 'symfony_demo_'.(++$i); |
| 54 | + } |
| 55 | + |
| 56 | + $this->projectName = $projectName; |
| 57 | + $this->projectDir = $this->projectDir.DIRECTORY_SEPARATOR.$projectName; |
| 58 | + } |
| 59 | + |
| 60 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 61 | + { |
| 62 | + try { |
| 63 | + $this |
| 64 | + ->download() |
| 65 | + ->extract() |
| 66 | + ->cleanUp() |
| 67 | + ->createGitIgnore() |
| 68 | + ->checkSymfonyRequirements() |
| 69 | + ->displayInstallationResult() |
| 70 | + ; |
| 71 | + } catch (\Exception $e) { |
| 72 | + $this->cleanUp(); |
| 73 | + throw $e; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes all the temporary files and directories created to |
| 79 | + * download the demo application. |
| 80 | + * |
| 81 | + * @return NewCommand |
| 82 | + */ |
| 83 | + private function cleanUp() |
| 84 | + { |
| 85 | + $this->fs->remove(dirname($this->downloadedFilePath)); |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * It displays the message with the result of installing the Symfony Demo |
| 92 | + * application and provides some pointers to the user. |
| 93 | + * |
| 94 | + * @return DemoCommand |
| 95 | + */ |
| 96 | + private function displayInstallationResult() |
| 97 | + { |
| 98 | + if (empty($this->requirementsErrors)) { |
| 99 | + $this->output->writeln(sprintf( |
| 100 | + " <info>%s</info> Symfony Demo Application was <info>successfully installed</info>. Now you can:\n", |
| 101 | + defined('PHP_WINDOWS_VERSION_BUILD') ? 'OK' : '✔' |
| 102 | + )); |
| 103 | + } else { |
| 104 | + $this->output->writeln(sprintf( |
| 105 | + " <comment>%s</comment> Symfony Demo Application was <info>successfully installed</info> but your system doesn't meet the\n". |
| 106 | + " technical requirements to run Symfony applications! Fix the following issues before executing it:\n", |
| 107 | + defined('PHP_WINDOWS_VERSION_BUILD') ? 'FAILED' : '✕' |
| 108 | + )); |
| 109 | + |
| 110 | + foreach ($this->requirementsErrors as $helpText) { |
| 111 | + $this->output->writeln(' * '.$helpText); |
| 112 | + } |
| 113 | + |
| 114 | + $this->output->writeln(sprintf( |
| 115 | + " After fixing these issues, re-check Symfony requirements executing this command:\n\n". |
| 116 | + " <comment>php %s/app/check.php</comment>\n\n". |
| 117 | + " Then, you can:\n", |
| 118 | + $this->projectName |
| 119 | + )); |
| 120 | + } |
| 121 | + |
| 122 | + $this->output->writeln(sprintf( |
| 123 | + " 1. Change your current directory to <comment>%s</comment>\n\n". |
| 124 | + " 2. Execute the <comment>php app/console server:run</comment> command to run the demo application.\n\n". |
| 125 | + " 3. Browse to the <comment>http://localhost:8000</comment> URL to see the demo application in action.\n\n", |
| 126 | + $this->projectDir |
| 127 | + )); |
| 128 | + |
| 129 | + return $this; |
| 130 | + } |
| 131 | +} |
0 commit comments