Skip to content

Commit e78d177

Browse files
authored
Multiple fixes (#2)
- Added template operation - Added optional file generation on example - Added ability to set root path for generation
1 parent 1ed126d commit e78d177

File tree

8 files changed

+167
-28
lines changed

8 files changed

+167
-28
lines changed

example/Form/FormGenerator.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Console\Style\SymfonyStyle;
1010

1111
use Alexecus\Spawner\Command\Command;
12+
use Alexecus\Spawner\Operations\Template;
1213
use Alexecus\Spawner\Operations\Copy;
1314

1415
use Alexecus\Spawner\Input\Validators\EmptyValidator;
@@ -20,14 +21,15 @@
2021
class FormGenerator extends Command
2122
{
2223
/**
23-
* @var Copy
24+
* @var Template
2425
*/
25-
private $copy;
26+
private $template;
2627

27-
public function __construct(Copy $copy)
28+
public function __construct(Template $template, Copy $copy)
2829
{
2930
parent::__construct();
3031

32+
$this->template = $template;
3133
$this->copy = $copy;
3234
}
3335

@@ -38,8 +40,7 @@ public function configure()
3840
{
3941
$this
4042
->setName('generate:form')
41-
->setDescription('Generates a new form plugin')
42-
;
43+
->setDescription('Generates a new form plugin');
4344
}
4445

4546
/**
@@ -51,19 +52,42 @@ public function execute(InputInterface $input, OutputInterface $output)
5152
new EmptyValidator('Form should not be empty'),
5253
]);
5354

54-
$name = $this->ask('What is the name of the form ?', 'ContactForm');
55+
// inputs
5556

57+
$name = $this->ask('What is the name of the form ?', 'ContactForm');
58+
$isTemplate = $this->confirm('Do you want to generate a template file ?');
5659
$confirm = $this->confirm('Do you confirm form generation ?');
5760

61+
// actions
62+
5863
if ($confirm) {
64+
if ($isTemplate) {
65+
$this->createTemplate($path, $name);
66+
}
67+
5968
$this->createForm($path, $name);
6069
$this->success("The form $name was generated");
6170
}
6271
}
6372

73+
/**
74+
* Creates the template file
75+
*
76+
* @param string $path The namespace path
77+
* @param string $name The name of the form
78+
*/
79+
private function createTemplate($path, $name)
80+
{
81+
$source = __DIR__ . '/template/template.html.twig';
82+
$target = "$path/$name.html.twig";
83+
84+
$this->copy->perform($source, $target);
85+
}
86+
6487
/**
6588
* Creates the form class
6689
*
90+
* @param string $path The namespace path
6791
* @param string $name The name of the form
6892
*/
6993
private function createForm($path, $name)
@@ -75,6 +99,6 @@ private function createForm($path, $name)
7599
'form_name' => $name,
76100
];
77101

78-
$this->copy->perform($source, $target, $replacements);
102+
$this->template->perform($source, $target, $replacements);
79103
}
80104
}

example/Form/info.yml

+18-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ form:
1717
arguments: ['Form']
1818

1919
- confirm:
20+
id: twig
21+
message: Do you want to have a twig template also ?
22+
23+
- ask:
24+
id: path
25+
message: What is the path ?
26+
defaults: /src/Form
27+
28+
- confirm:
29+
id: confirm
2030
message: Do you confirm generation ?
31+
terminate: yes
2132

2233
actions:
23-
- copy:
34+
- template:
2435
source: template/Form.php.twig
25-
target: src/Form/{form_name}.php
36+
target: {path}/{name}.php
2637
replacements:
2738
form_name: name
39+
40+
- copy:
41+
if: twig
42+
source: template/template.twig
43+
target: {path}/template.twig
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<p>This is a form template</p>
3+
</div>

example/generate renamed to example/console

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ require __DIR__ . '/../vendor/autoload.php';
55

66
$app = new \Alexecus\Spawner\Application();
77

8+
$app->setRoot(__DIR__);
9+
810
$app->add(\Alexecus\Example\Form\FormGenerator::class);
911

1012
$app->run();

src/Application.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@
1010
*/
1111
class Application
1212
{
13+
private $commands = [];
14+
1315
public function __construct($name = 'Spawner', $version = '1.0')
1416
{
1517
$this->console = new Console($name, $version);
1618
$this->container = new Container();
19+
$this->path = new Path();
20+
}
21+
22+
/**
23+
* Sets the root path for generation
24+
*
25+
* @param string $path
26+
*/
27+
public function setRoot($path)
28+
{
29+
$this->path->setRoot($path);
1730
}
1831

1932
/**
@@ -23,16 +36,22 @@ public function __construct($name = 'Spawner', $version = '1.0')
2336
*/
2437
public function add($command)
2538
{
26-
$this->console->add(
27-
$this->container->get($command)
28-
);
39+
$this->commands[] = $command;
2940
}
3041

3142
/**
3243
* Runs the application kernel
3344
*/
3445
public function run()
3546
{
47+
$this->container->set(Path::class, $this->path);
48+
49+
foreach ($this->commands as $command) {
50+
$this->console->add(
51+
$this->container->get($command)
52+
);
53+
}
54+
3655
$this->console->run();
3756
}
3857
}

src/Operations/Copy.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
use Symfony\Component\Filesystem\Filesystem;
66

77
use Alexecus\Spawner\Path;
8-
use Alexecus\Spawner\Render\Twig;
98

109
class Copy
1110
{
12-
/**
13-
* @var Twig
14-
*/
15-
private $twig;
16-
1711
/**
1812
* @var Filesystem
1913
*/
@@ -24,9 +18,8 @@ class Copy
2418
*/
2519
private $path;
2620

27-
public function __construct(Twig $twig, Filesystem $filesystem, Path $path)
21+
public function __construct(Filesystem $filesystem, Path $path)
2822
{
29-
$this->twig = $twig;
3023
$this->filesystem = $filesystem;
3124
$this->path = $path;
3225
}
@@ -36,14 +29,11 @@ public function __construct(Twig $twig, Filesystem $filesystem, Path $path)
3629
*
3730
* @param string $source The file path
3831
* @param string $target The path to copy this file into
39-
* @param array $replacements Replacement data for twig
4032
*/
41-
public function perform($source, $target, $replacements)
33+
public function perform($source, $target)
4234
{
43-
$root = $this->path->root();
44-
$body = $this->twig->fromFile($source, $replacements);
45-
$destination = $root . '/' . $target;
35+
$destination = $this->path->absolute($target);
4636

47-
$this->filesystem->dumpFile($destination, $body);
37+
$this->filesystem->copy($source, $destination);
4838
}
4939
}

src/Operations/Template.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Alexecus\Spawner\Operations;
4+
5+
use Symfony\Component\Filesystem\Filesystem;
6+
7+
use Alexecus\Spawner\Path;
8+
use Alexecus\Spawner\Render\Twig;
9+
10+
class Template
11+
{
12+
/**
13+
* @var Twig
14+
*/
15+
private $twig;
16+
17+
/**
18+
* @var Filesystem
19+
*/
20+
private $filesystem;
21+
22+
/**
23+
* @var Path
24+
*/
25+
private $path;
26+
27+
public function __construct(Twig $twig, Filesystem $filesystem, Path $path)
28+
{
29+
$this->twig = $twig;
30+
$this->filesystem = $filesystem;
31+
$this->path = $path;
32+
}
33+
34+
/**
35+
* Performs the copy operation
36+
*
37+
* @param string $source The file path
38+
* @param string $target The path to copy this file into
39+
* @param array $replacements Replacement data for twig
40+
*/
41+
public function perform($source, $target, $replacements = [])
42+
{
43+
$body = $this->twig->fromFile($source, $replacements);
44+
$destination = $this->path->absolute($target);
45+
46+
$this->filesystem->dumpFile($destination, $body);
47+
}
48+
}

src/Path.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,47 @@
22

33
namespace Alexecus\Spawner;
44

5+
/**
6+
* Handles generation of paths
7+
*/
58
class Path
69
{
7-
public function root()
10+
/**
11+
* @var string
12+
*/
13+
private $root;
14+
15+
/**
16+
* Set the root path
17+
*
18+
* @param string $root The absolute root path
19+
*
20+
* @return
21+
*/
22+
public function setRoot($root)
23+
{
24+
$this->root = $root;
25+
}
26+
27+
/**
28+
* Gets the root path
29+
*
30+
* @return string
31+
*/
32+
public function getRoot()
33+
{
34+
return $this->root ?? getcwd();
35+
}
36+
37+
/**
38+
* Generate an absolute path relative to the root
39+
*
40+
* @param string $path The relative path to generate from
41+
*
42+
* @return string
43+
*/
44+
public function absolute($path)
845
{
9-
return getcwd();
46+
return $this->getRoot() . '/' . ltrim($path, '/');
1047
}
1148
}

0 commit comments

Comments
 (0)