-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWpcli.php
82 lines (72 loc) · 2.15 KB
/
Wpcli.php
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
<?php declare(strict_types=1);
/**
* Copyright (C) Apis Networks, Inc - All Rights Reserved.
*
* Unauthorized copying of this file, via any medium, is
* strictly prohibited without consent. Any dissemination of
* material herein is prohibited.
*
* For licensing inquiries email <[email protected]>
*
* Written by Matt Saladna <[email protected]>, July 2020
*/
namespace Module\Support\Webapps\App\Type\Wordpress;
use Module\Support\Webapps\PhpWrapper;
use Symfony\Component\Yaml\Yaml;
class Wpcli extends PhpWrapper
{
// primary domain document root
const BIN = '/usr/share/pear/wp-cli.phar';
// latest release
const DOWNLOAD_URL = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar';
const CONFIG_PATH = '.wp-cli/config.yml';
/**
* Run command against wp-cli
*
* @param string|null $path
* @param string $cmd
* @param array $args
* @param array $env
* @return array|bool
*/
public function exec(?string $path, string $cmd, array $args = [], array $env = []): array
{
if (is_debug()) {
$cmd = '--debug ' . $cmd;
}
if ($path) {
$cmd = '--path=%(path)s ' . $cmd;
$args['path'] = $path;
}
$ret = parent::exec($path, self::BIN . ' ' . $cmd, $args, ['SERVER_NAME' => $this->getAuthContext()->domain] + $env);
// $from_email isn't always set, ensure WP can send via wp-includes/pluggable.php
if (0 === strncmp(coalesce($ret['stderr'], $ret['stdout']), 'Error:', 6)) {
// move stdout to stderr on error for consistency
$ret['success'] = false;
if (!$ret['stderr']) {
$ret['stderr'] = $ret['stdout'];
}
}
return $ret;
}
/**
* Update WP-CLI configuration
*
* @param array $vals
* @return bool
*/
public function setConfiguration(array $vals): bool
{
$path = $this->user_get_home() . '/' . self::CONFIG_PATH;
if (!$this->file_exists(\dirname($path))) {
$this->file_create_directory(\dirname($path));
}
$raw = '';
if ($this->file_exists($path)) {
$raw = $this->file_get_file_contents($path);
}
$yaml = Yaml::parse($raw);
$yaml = array_merge((array)$yaml, $vals);
return $this->file_put_file_contents($path, Yaml::dump($yaml)) > 0;
}
}