Conditions | 13 |
Paths | 380 |
Total Lines | 93 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
71 | protected function execute(InputInterface $input, OutputInterface $output) |
||
72 | { |
||
73 | $force = $input->getOption('force'); |
||
74 | $demo = $input->getOption('demo'); |
||
75 | |||
76 | try { |
||
77 | // ask to override existing site? |
||
78 | if (Util\File::getFS()->exists(Util::joinFile($this->getPath(false), $this->locateConfigFile($this->getPath())['name'] ?: self::CONFIG_FILE[0])) && !$force) { |
||
79 | $output->writeln('<comment>Website already exists.</comment>'); |
||
80 | if (!$this->io->confirm('Do you want to override it?', false)) { |
||
81 | return 0; |
||
82 | } |
||
83 | } |
||
84 | // define root path |
||
85 | $root = Util\Platform::isPhar() ? Util\Platform::getPharPath() . '/' : realpath(Util::joinFile(__DIR__, '/../../')); |
||
86 | // setup questions |
||
87 | $title = $this->io->ask('Give a title to your website', 'New website'); |
||
88 | $baseline = $this->io->ask('Give a baseline to your website', ''); |
||
89 | $baseurl = $this->io->ask('Base URL?', '/', [$this, 'validateUrl']); |
||
90 | $description = $this->io->ask('Write a full description of your site', 'Website created with Cecil.'); |
||
91 | $authorName = $this->io->ask('What is the author name?', 'Cecil'); |
||
92 | $authorUrl = $this->io->ask('What is the author URL?', 'https://cecil.app', [$this, 'validateUrl']); |
||
93 | $demo = ($demo !== false) ?: $this->io->confirm('Add demo content?', false); |
||
94 | // override skeleton default config |
||
95 | $config = Yaml::parseFile(Util::joinPath($root, 'resources/skeleton', self::CONFIG_FILE[0]), Yaml::PARSE_DATETIME); |
||
96 | $config = array_replace_recursive($config, [ |
||
97 | 'title' => $title, |
||
98 | 'baseline' => $baseline, |
||
99 | 'baseurl' => $baseurl, |
||
100 | 'description' => $description, |
||
101 | 'author' => [ |
||
102 | 'name' => $authorName, |
||
103 | 'url' => $authorUrl, |
||
104 | ], |
||
105 | ]); |
||
106 | $configYaml = Yaml::dump($config, 2, 2); |
||
107 | Util\File::getFS()->dumpFile(Util::joinPath($this->getPath(), $this->locateConfigFile($this->getPath())['name'] ?: self::CONFIG_FILE[0]), $configYaml); |
||
108 | // create path dir |
||
109 | Util\File::getFS()->mkdir($this->getPath(false)); |
||
110 | // creates sub dir |
||
111 | foreach ( |
||
112 | [ |
||
113 | (string) $this->getBuilder()->getConfig()->get('assets.dir'), |
||
114 | (string) $this->getBuilder()->getConfig()->get('layouts.dir'), |
||
115 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
116 | (string) $this->getBuilder()->getConfig()->get('static.dir'), |
||
117 | ] as $value |
||
118 | ) { |
||
119 | Util\File::getFS()->mkdir(Util::joinPath($this->getPath(), $value)); |
||
120 | } |
||
121 | // copy files |
||
122 | foreach ( |
||
123 | [ |
||
124 | 'assets/favicon.png', |
||
125 | 'pages/index.md', |
||
126 | 'static/cecil-card.png', |
||
127 | ] as $value |
||
128 | ) { |
||
129 | Util\File::getFS()->copy( |
||
130 | Util::joinPath($root, 'resources/skeleton', $value), |
||
131 | Util::joinPath($this->getPath(), $value) |
||
132 | ); |
||
133 | } |
||
134 | // demo: copy all files |
||
135 | if ($demo) { |
||
136 | foreach ( |
||
137 | [ |
||
138 | (string) $this->getBuilder()->getConfig()->get('assets.dir'), |
||
139 | (string) $this->getBuilder()->getConfig()->get('layouts.dir'), |
||
140 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
141 | (string) $this->getBuilder()->getConfig()->get('static.dir'), |
||
142 | ] as $value |
||
143 | ) { |
||
144 | Util\File::getFS()->mirror( |
||
145 | Util::joinPath($root, 'resources/skeleton', $value), |
||
146 | Util::joinPath($this->getPath(), $value) |
||
147 | ); |
||
148 | } |
||
149 | } |
||
150 | // done |
||
151 | $output->writeln(\sprintf('<info>Your new website is created in %s.</info>', realpath($this->getPath()))); |
||
152 | $this->io->newLine(); |
||
153 | $this->io->listing([ |
||
154 | 'Start the built-in preview server with <info>' . $this->binName() . ' serve</info>', |
||
155 | 'You can create a new page with <info>' . $this->binName() . ' new:page</info>', |
||
156 | ]); |
||
157 | |||
158 | $this->io->text('Visit <href=https://cecil.app>https://cecil.app</> for documentation and more.'); |
||
159 | } catch (\Exception $e) { |
||
160 | throw new RuntimeException(\sprintf($e->getMessage())); |
||
161 | } |
||
162 | |||
163 | return 0; |
||
164 | } |
||
166 |