Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace LakeDrops\Behat4Drupal;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Script\Event;
use LakeDrops\Component\Dotenv\Dotenv;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
/**
* Class Handler.
*
* @package LakeDrops\Behat4Drupal
*/
class Handler {
/**
* The composer object running this session.
*
* @var \Composer\Composer
*/
protected $composer;
/**
* The input-output object for the composer session.
*
* @var \Composer\IO\IOInterface
*/
protected $io;
/**
* Handler constructor.
*
* @param \Composer\Composer $composer
* The composer object.
* @param \Composer\IO\IOInterface $io
* The input-output object.
*/
public function __construct(Composer $composer, IOInterface $io) {
$this->composer = $composer;
$this->io = $io;
}
/**
* Post project create event to execute the scaffolding.
*
* @param \Composer\Script\Event $event
* The event that triggered the plugin.
* @param bool $overwrite
* Whether to overwrite existing config files.
*/
public function configureProject(Event $event, $overwrite = FALSE) {
// We only do the fancy stuff for developers.
if (!$event->isDevMode()) {
return;
}
$options = $this->getOptions();
$fs = new Filesystem();
$installationManager = $this->composer->getInstallationManager();
// Directory where the root project is being created.
$projectRoot = getcwd();
// Directory where this plugin is being installed.
$pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/behat4drupal'));
// Provide all the required files.
$twig_loader = new \Twig_Loader_Array([]);
$twig = new \Twig_Environment($twig_loader);
foreach ($this->getFiles($projectRoot) as $template => $def) {
if (!$fs->exists($def['dest'])) {
$fs->mkdir($def['dest']);
}
$twig_loader->setTemplate($template, $template);
$filename = $twig->render($template, $options);
$file = $def['dest'] . '/' . $filename;
if (($overwrite && empty($def['custom'])) || !$fs->exists($file)) {
$twig_loader->setTemplate($filename, file_get_contents($pluginRoot . '/templates/' . $template . '.twig'));
$rendered = $twig->render($filename, $options);
if (!empty($def['add2yaml']) && isset($options[$filename])) {
$yaml = Yaml::parse($rendered);
$yaml = array_merge_recursive($yaml, $options[$filename]);
$rendered = Yaml::dump($yaml, 9, 2);
}
if ($fs->exists($file)) {
if (md5_file($file) == md5($rendered)) {
continue;
}
$orig_file = $file . '.orig';
if ($fs->exists($orig_file)) {
$fs->remove($orig_file);
}
$fs->rename($file, $orig_file);
}
file_put_contents($file, $rendered);
}
$fs->chmod($file, 0664);
}
}
/**
* List of files and settings on how to handle them.
*
* @param string $projectRoot
* Name of the project's root directory.
*
* @return array
* List of files.
*/
protected function getFiles($projectRoot) {
return [
'behat.yml' => [
'dest' => $projectRoot . '/tests/behat',
'add2yaml' => TRUE,
],
'anonymous.feature' => [
'dest' => $projectRoot . '/tests/behat/features/basic',
],
'FeatureContext.php' => [
'dest' => $projectRoot . '/tests/behat/bootstrap/context',
'custom' => TRUE,
],
];
}
/**
* Retrieve configuration for this package.
*
* @return array
* The settings from the extra configuration.
*/
protected function getOptions() {
$projectname = str_replace([' ', '-', '_', '.'], '', basename(getcwd()));
$env = new Dotenv('behat4drupal', $this->io);
$env->put('COMPOSE_PROJECT_NAME', $projectname);
$extra = $this->composer->getPackage()->getExtra() + ['behat4drupal' => []];
return $extra['behat4drupal'] + [
'projectname' => $projectname,
];
}
}