1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Command; |
10
|
|
|
|
11
|
|
|
use Cecil\Util\Plateform; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
18
|
|
|
use Symfony\Component\Process\Process; |
19
|
|
|
|
20
|
|
|
class NewPage extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
->setName('new:page') |
29
|
|
|
->setDescription('Create a new page') |
30
|
|
|
->setDefinition( |
31
|
|
|
new InputDefinition([ |
32
|
|
|
new InputArgument('name', InputArgument::REQUIRED, 'New page name'), |
33
|
|
|
new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'), |
34
|
|
|
new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override the file if already exist'), |
35
|
|
|
new InputOption('open', 'o', InputOption::VALUE_NONE, 'Open editor automatically'), |
36
|
|
|
]) |
37
|
|
|
) |
38
|
|
|
->setHelp('Create a new page file (with a default title and the current date).'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$this->name = $input->getArgument('name'); |
|
|
|
|
47
|
|
|
$this->force = $input->getOption('force'); |
|
|
|
|
48
|
|
|
$this->open = $input->getOption('open'); |
|
|
|
|
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
// file name (without extension) |
52
|
|
|
if (false !== $extPos = strripos($this->name, '.md')) { |
|
|
|
|
53
|
|
|
$this->name = substr($this->name, 0, $extPos); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
// path |
56
|
|
|
$fileRelativePath = $this->getBuilder($output)->getConfig()->get('content.dir').'/'.$this->name.'.md'; |
|
|
|
|
57
|
|
|
$filePath = $this->getPath().'/'.$fileRelativePath; |
58
|
|
|
|
59
|
|
|
// file already exists? |
60
|
|
View Code Duplication |
if ($this->fs->exists($filePath) && !$this->force) { |
|
|
|
|
61
|
|
|
$helper = $this->getHelper('question'); |
62
|
|
|
$question = new ConfirmationQuestion(sprintf('This page already exists. Do you want to override it? [y/n]', $this->getpath()), false); |
63
|
|
|
if (!$helper->ask($input, $output, $question)) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// create new file |
69
|
|
|
$title = $this->name; |
|
|
|
|
70
|
|
|
if (false !== strrchr($this->name, '/')) { |
|
|
|
|
71
|
|
|
$title = substr(strrchr($this->name, '/'), 1); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
$date = date('Y-m-d'); |
74
|
|
|
$fileContent = str_replace(['%title%', '%date%'], [$title, $date], $this->findModel($this->name)); |
|
|
|
|
75
|
|
|
$this->fs->dumpFile($filePath, $fileContent); |
76
|
|
|
|
77
|
|
|
$output->writeln(sprintf('File "%s" created.', $fileRelativePath)); |
78
|
|
|
|
79
|
|
|
// open editor? |
80
|
|
|
if ($this->open) { |
81
|
|
|
if (!$this->hasEditor()) { |
82
|
|
|
$output->writeln('<comment>No editor configured.</comment>'); |
83
|
|
|
} |
84
|
|
|
$this->openEditor($filePath); |
85
|
|
|
} |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return 0; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Find the page model and return its content. |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function findModel($name) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$section = strstr($this->name, '/', true); |
|
|
|
|
103
|
|
|
if ($section && file_exists($model = sprintf('%s/models/%s.md', $this->getPath(), $section))) { |
104
|
|
|
return file_get_contents($model); |
105
|
|
|
} |
106
|
|
|
if (file_exists($model = sprintf('%s/models/default.md', $this->getPath()))) { |
107
|
|
|
return file_get_contents($model); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return <<<'EOT' |
111
|
|
|
--- |
112
|
|
|
title: '%title%' |
113
|
|
|
date: '%date%' |
114
|
|
|
draft: true |
115
|
|
|
--- |
116
|
|
|
|
117
|
|
|
_[Your content here]_ |
118
|
|
|
EOT; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Editor is configured? |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected function hasEditor() |
127
|
|
|
{ |
128
|
|
|
if ($this->builder->getConfig()->get('editor')) { |
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Open new file in editor (if configured). |
137
|
|
|
* |
138
|
|
|
* @param string $filePath |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
protected function openEditor($filePath) |
143
|
|
|
{ |
144
|
|
|
if ($editor = $this->builder->getConfig()->get('editor')) { |
145
|
|
|
switch ($editor) { |
146
|
|
|
case 'typora': |
147
|
|
|
if (Plateform::getOS() == Plateform::OS_OSX) { |
148
|
|
|
$command = sprintf('open -a typora "%s"', $filePath); |
149
|
|
|
} |
150
|
|
|
break; |
151
|
|
|
default: |
152
|
|
|
$command = sprintf('%s "%s"', $editor, $filePath); |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
$process = new Process($command); |
|
|
|
|
156
|
|
|
$process->run(); |
157
|
|
|
if (!$process->isSuccessful()) { |
158
|
|
|
throw new \Exception(sprintf('Can\'t run "%s".', $command)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.