1
+ <?php
2
+
3
+ namespace Couscous \Application \Cli ;
4
+
5
+ use Symfony \Component \Console \Command \Command ;
6
+ use Symfony \Component \Console \Input \InputArgument ;
7
+ use Symfony \Component \Console \Input \InputInterface ;
8
+ use Symfony \Component \Console \Output \OutputInterface ;
9
+
10
+ /**
11
+ * Initialize a new Couscous template.
12
+ *
13
+ * @author Ross J. Hagan <[email protected] >
14
+ */
15
+ class InitTemplateCommand extends Command {
16
+
17
+ protected function configure ()
18
+ {
19
+ $ this
20
+ ->setName ('init:template ' )
21
+ ->setDescription ('Initialize a new Couscous template ' );
22
+
23
+ $ this ->addArgument (
24
+ 'template_name ' ,
25
+ InputArgument::REQUIRED ,
26
+ 'Template name '
27
+ )
28
+ ->addArgument (
29
+ 'directory ' ,
30
+ InputArgument::OPTIONAL ,
31
+ 'Directory name ' ,
32
+ 'website '
33
+ );
34
+ }
35
+
36
+ protected function execute (InputInterface $ input , OutputInterface $ output )
37
+ {
38
+
39
+ $ fileExtension = '.twig ' ;
40
+
41
+ $ dirName = $ input ->getArgument ('directory ' );
42
+ $ directory = getcwd () . '/ ' . $ dirName . '/ ' ;
43
+ $ templateName = $ input ->getArgument ('template_name ' ) . $ fileExtension ;
44
+
45
+ $ fileLocation = $ directory . $ templateName ;
46
+ $ fileExists = file_exists ($ fileLocation );
47
+
48
+ if (! file_exists (getcwd () . '/ ' . $ dirName )) {
49
+ $ output ->writeln ('<comment>Creating directory.</comment> ' );
50
+ mkdir (getcwd () . '/ ' . $ dirName );
51
+ }
52
+
53
+ if ($ fileExists ) {
54
+ $ output ->writeln ('<error>That template exists at ' . $ fileLocation . ', so nothing has been changed.</error> ' );
55
+ $ output ->writeln ('<error>Try another name!</error> ' );
56
+ return ;
57
+ }
58
+
59
+ if (! $ fileExists ) {
60
+ $ output ->writeln ('<comment>Initialising template.</comment> ' );
61
+ $ template = <<<HTML
62
+ <!DOCTYPE html>
63
+ <html>
64
+ <head>
65
+ <title>My project!</title>
66
+ </head>
67
+ <body>
68
+
69
+ {% block content %}
70
+
71
+ <p>
72
+ Don't forget you can add variables into your YAML front matter, or in your couscous.yml
73
+ then use them inside double curly braces!
74
+ </p>
75
+
76
+ <p>
77
+ Also, set up a baseUrl in your couscous.yml and use it
78
+ to <a href="{{ baseUrl }}/link/in/the/site">link to another page</a> in your site
79
+ </p>
80
+
81
+ {{ content|raw }}
82
+
83
+ {% endblock %}
84
+
85
+ </body>
86
+ </html>
87
+ HTML ;
88
+ file_put_contents ($ fileLocation , $ template );
89
+ }
90
+
91
+ }
92
+
93
+ }
0 commit comments