|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the PHPoole/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Process\Process; |
|
14
|
|
|
use Zend\Console\Prompt\Confirm; |
|
15
|
|
|
|
|
16
|
|
|
class NewPage extends AbstractCommand |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $name; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $force; |
|
26
|
|
|
|
|
27
|
|
|
public function processCommand() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->name = $this->getRoute()->getMatchedParam('name'); |
|
30
|
|
|
$this->force = $this->getRoute()->getMatchedParam('force', false); |
|
31
|
|
|
|
|
32
|
|
|
try { |
|
33
|
|
|
// file name (without extension) |
|
34
|
|
|
if (false !== $extPos = strripos($this->name, '.md')) { |
|
35
|
|
|
$this->name = substr($this->name, 0, $extPos); |
|
36
|
|
|
} |
|
37
|
|
|
// path |
|
38
|
|
|
$fileRelativePath = $this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md'; |
|
39
|
|
|
$filePath = $this->getPath().'/'.$fileRelativePath; |
|
40
|
|
|
|
|
41
|
|
|
// file already exists? |
|
42
|
|
|
if ($this->fs->exists($filePath) && !$this->force) { |
|
43
|
|
|
if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) { |
|
44
|
|
|
exit(0); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// create new file |
|
49
|
|
|
$title = $this->name; |
|
50
|
|
|
if (false !== strrchr($this->name, '/')) { |
|
51
|
|
|
$title = substr(strrchr($this->name, '/'), 1); |
|
52
|
|
|
} |
|
53
|
|
|
$date = date('Y-m-d'); |
|
54
|
|
|
$fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findArchetype($this->name)); |
|
55
|
|
|
$this->fs->dumpFile($filePath, $fileContent); |
|
56
|
|
|
$this->wlDone(sprintf('File "%s" created!', $fileRelativePath)); |
|
57
|
|
|
// open editor? |
|
58
|
|
|
$this->openEditor($filePath); |
|
59
|
|
|
} catch (\Exception $e) { |
|
60
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Find the archtetype and return its content. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function findArchetype($name) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$section = strstr($this->name, '/', true); |
|
74
|
|
|
if ($section && file_exists($archetype = sprintf('%s/archetypes/%s.md', $this->getPath(), $section))) { |
|
75
|
|
|
return file_get_contents($archetype); |
|
76
|
|
|
} |
|
77
|
|
|
if (file_exists($archetype = sprintf('%s/archetypes/default.md', $this->getPath()))) { |
|
78
|
|
|
return file_get_contents($archetype); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return <<<'EOT' |
|
82
|
|
|
--- |
|
83
|
|
|
title: '%title%' |
|
84
|
|
|
date: '%date%' |
|
85
|
|
|
draft: true |
|
86
|
|
|
--- |
|
87
|
|
|
|
|
88
|
|
|
EOT; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Open new file in editor (if configured). |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $filePath |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function openEditor($filePath) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($editor = $this->phpoole->getConfig()->get('editor')) { |
|
101
|
|
|
$command = sprintf('%s %s', $editor, $filePath); |
|
102
|
|
|
$process = new Process($command); |
|
103
|
|
|
$process->run(); |
|
104
|
|
|
if (!$process->isSuccessful()) { |
|
105
|
|
|
throw new \Exception(sprintf("Can't open '%s' editor.", $editor)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.