-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathTrainingCaseHandler.php
More file actions
131 lines (102 loc) · 3.47 KB
/
Copy pathTrainingCaseHandler.php
File metadata and controls
131 lines (102 loc) · 3.47 KB
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
<?php
namespace drupal;
use SDK\Build\PGO\Abstracts;
use SDK\Build\PGO\Interfaces;
use SDK\Build\PGO\Config;
use SDK\Build\PGO\PHP;
use SDK\Exception;
use SDK\Build\PGO\Tool;
class TrainingCaseHandler extends Abstracts\TrainingCase implements Interfaces\TrainingCase
{
/** @var string */
protected $base;
/** @var ?Interfaces\Server\HTTP $nginx */
protected $nginx;
/** @var mixed */
protected $php;
/** @var int */
protected $max_runs = 8;
public function __construct(Config $conf, ?Interfaces\Server\HTTP $nginx, ?Interfaces\Server\DB $maria)
{
if (!$nginx) {
throw new Exception("Invalid NGINX object");
}
$this->conf = $conf;
$this->base = $this->conf->getCaseWorkDir($this->getName());
$this->nginx = $nginx;
$this->php = $nginx->getPhp();
}
public function getName() : string
{
return __NAMESPACE__;
}
public function getJobFilename() : string
{
return $this->conf->getJobDir() . DIRECTORY_SEPARATOR . $this->getName() . ".txt";
}
protected function getToolFn() : string
{
return $this->base . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array("vendor", "drush", "drush", "drush"));
}
protected function setupDist() : void
{
$db_path = str_replace("\\", "/", $this->base) . "/drupal.sqlite3";
if (!file_exists($db_path)) {
echo "Setting up in '{$this->base}'\n";
$php = new PHP\CLI($this->conf);
$cmd = $this->getToolFn() . " site-install demo_umami --db-url=sqlite://$db_path --account-name=admin --account-pass=adminpass --yes";
$php->exec($cmd);
}
$port = $this->getHttpPort();
$host = $this->getHttpHost();
$vars = array(
$this->conf->buildTplVarName($this->getName(), "docroot") => str_replace("\\", "/", $this->base . DIRECTORY_SEPARATOR . "web"),
);
$tpl_fn = $this->conf->getCasesTplDir($this->getName()) . DIRECTORY_SEPARATOR . "nginx.partial.conf";
$this->nginx->addServer($tpl_fn, $vars);
}
/** @return void */
public function setupUrls()
{
$this->nginx->up();
$url = "http://" . $this->getHttpHost() . ":" . $this->getHttpPort();
$s = file_get_contents($url);
$this->nginx->down(true);
echo "Generating training urls.\n";
$lst = array();
if (preg_match_all(", href=\"([^\"]+)\",", $s, $m)) {
foreach ($m[1] as $u) {
$p = parse_url($u, PHP_URL_PATH);
if (strlen($p) >= 2 && "/" == $p[0] && "/" != $p[1] && !in_array(substr($p, -3), array("css", "xml", "ico"))) {
$ur = "http://" . $this->getHttpHost() . ":" . $this->getHttpPort() . $u;
if (!in_array($ur, $lst)) {
$lst[] = $ur;
}
}
}
}
if (empty($lst)) {
printf("\033[31m WARNING: Training URL list is empty, check the regex and the possible previous error messages!\033[0m\n");
}
$fn = $this->getJobFilename();
$s = implode("\n", $lst);
if (strlen($s) !== file_put_contents($fn, $s)) {
throw new Exception("Couldn't write '$fn'.");
}
}
public function prepareInit(Tool\PackageWorkman $pw, bool $force = false) : void
{
$php = new PHP\CLI($this->conf);
$composer = $this->conf->getToolsDir() . DIRECTORY_SEPARATOR . "composer.phar";
$cmd = $composer . " create-project drupal-composer/drupal-project:8.x-dev {$this->base} --stability dev --no-interaction";
$php->exec($cmd);
}
public function init() : void
{
echo "Initializing " . $this->getName() . ".\n";
$this->setupDist();
$this->setupUrls();
echo $this->getName() . " initialization done.\n";
echo $this->getName() . " site configured to run under " . $this->getHttpHost() . ":" . $this->getHttpPort() . "\n";
}
}