1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PHPoole 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 PHPoole\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
|
|
|
// todo: should use config array instead of just '.md' |
35
|
|
|
if (false !== $extPos = strripos($this->name, '.md')) { |
36
|
|
|
$this->name = substr($this->name, 0, $extPos); |
37
|
|
|
} |
38
|
|
|
// find archetype |
39
|
|
|
$section = strstr($this->name, '/', true); |
40
|
|
|
$fileContent = <<<'EOT' |
41
|
|
|
--- |
42
|
|
|
title: '%title%' |
43
|
|
|
date: '%date%' |
44
|
|
|
draft: true |
45
|
|
|
--- |
46
|
|
|
|
47
|
|
|
EOT; |
48
|
|
|
if ($section && file_exists($archetype = sprintf('%s/archetypes/%s.md', $this->getPath(), $section))) { |
49
|
|
|
$fileContent = file_get_contents($archetype); |
50
|
|
|
} else { |
51
|
|
|
if (file_exists($archetype = sprintf('%s/archetypes/default.md', $this->getPath()))) { |
52
|
|
|
$fileContent = file_get_contents($archetype); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
// path |
56
|
|
|
$fileRelativePath = $this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md'; |
57
|
|
|
$filePath = $this->getPath().'/'.$fileRelativePath; |
58
|
|
|
// file already exists? |
59
|
|
|
if ($this->fs->exists($filePath) && !$this->force) { |
60
|
|
|
if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) { |
61
|
|
|
exit(0); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
// create new file |
65
|
|
|
$title = $this->name; |
66
|
|
|
if (false !== strrchr($this->name, '/')) { |
67
|
|
|
$title = substr(strrchr($this->name, '/'), 1); |
68
|
|
|
} |
69
|
|
|
$date = date('Y-m-d'); |
70
|
|
|
$fileContent = str_replace(['%title%', '%date%'], [$title, $date], $fileContent); |
71
|
|
|
$this->fs->dumpFile($filePath, $fileContent); |
72
|
|
|
$this->wlDone(sprintf('File "%s" created!', $fileRelativePath)); |
73
|
|
|
// open editor? |
74
|
|
|
if ($editor = $this->phpoole->getConfig()->get('editor')) { |
75
|
|
|
$command = sprintf('%s %s', $editor, $filePath); |
76
|
|
|
$process = new Process($command); |
77
|
|
|
$process->run(); |
78
|
|
|
if (!$process->isSuccessful()) { |
79
|
|
|
throw new \Exception(sprintf("Can't open '%s' editor.", $editor)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} catch (\Exception $e) { |
83
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|