Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format code with php-cs-fixer #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 43 additions & 69 deletions src/lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class Bedrock_Module extends \Wordpress_Module

protected function getAppRoot(string $hostname, string $path = ''): ?string
{
if (file_exists($tmp = $this->getDocumentRoot($hostname, $path) . '/wp-config.php'))
{
if (file_exists($tmp = $this->getDocumentRoot($hostname, $path) . '/wp-config.php')) {
return $tmp;
}

Expand All @@ -36,19 +35,14 @@ protected function getAppRoot(string $hostname, string $path = ''): ?string

protected function getAppRootPath(string $hostname, string $path = ''): ?string
{
if ($hostname[0] === '/')
{
if (!($path = realpath($this->domain_fs_path($hostname))))
{
if ($hostname[0] === '/') {
if (!($path = realpath($this->domain_fs_path($hostname)))) {
return null;
}
$approot = \dirname($path);
}
else
{
} else {
$approot = $this->getAppRoot($hostname, $path);
if (!$approot)
{
if (!$approot) {
return null;
}
$approot = $this->domain_fs_path($approot);
Expand All @@ -66,8 +60,7 @@ protected function getAppRootPath(string $hostname, string $path = ''): ?string
*/
protected function parseLock(string $lockType, string $version): string
{
switch ($lockType)
{
switch ($lockType) {
case 'major':
return '~' . Versioning::asMinor($version);
case 'minor':
Expand Down Expand Up @@ -98,11 +91,9 @@ protected function generateNewConfiguration(string $hostname, string $docroot, D
'set DB_PASSWORD' => ["dotenv set DB_PASSWORD '%(password)s'", ['password' => $dbcredentials->password]],
];

foreach ($steps as $name => $actions)
{
foreach ($steps as $name => $actions) {
$ret = $this->execCommand($docroot, $actions[0], $actions[1]);
if (!$ret['success'])
{
if (!$ret['success']) {
return error('failed to %s, error: %s', $name, coalesce($hostname, $docroot, $ret['stderr'], $ret['stdout']));
}
}
Expand All @@ -120,40 +111,36 @@ protected function generateNewConfiguration(string $hostname, string $docroot, D
*/
public function install(string $hostname, string $path = '', array $opts = array()): bool
{
if (!$this->mysql_enabled())
{
return error('%(what)s must be enabled to install %(app)s',
['what' => 'MySQL', 'app' => static::APP_NAME]);
if (!$this->mysql_enabled()) {
return error(
'%(what)s must be enabled to install %(app)s',
['what' => 'MySQL', 'app' => static::APP_NAME]
);
}

if (!$this->php_composer_exists())
{
if (!$this->php_composer_exists()) {
return error('composer missing! contact sysadmin');
}

// Same situation as with Ghost. We can't install under a path for fear of
// leaking information
if ($path)
{
if ($path) {
return error('Composer projects may only be installed directly on a subdomain or domain without a child path, e.g. https://domain.com but not https://domain.com/laravel');
}

if (!($docroot = $this->getDocumentRoot($hostname, $path)))
{
if (!($docroot = $this->getDocumentRoot($hostname, $path))) {
return error("failed to normalize path for `%s'", $hostname);
}

if (!$this->parseInstallOptions($opts, $hostname, $path))
{
if (!$this->parseInstallOptions($opts, $hostname, $path)) {
return false;
}

// Install dotenv command
$ret = $this->execCommand($docroot, 'package install %(dotenvcmd)s ', [
'dotenvcmd' => static::DOTENV_COMMAND,
]);
if (!$ret['success'])
{
if (!$ret['success']) {
return error('failed to install dotenv command, error: %s', coalesce($ret['stderr'], $ret['stdout']));
}

Expand All @@ -166,41 +153,37 @@ public function install(string $hostname, string $path = '', array $opts = array
]);

// Rollback on failure
if (!$ret['success'])
{
if (!$ret['success']) {
$this->file_delete($docroot, true);

return error('failed to download roots/bedrock package: %s %s',
$ret['stderr'], $ret['stdout']
return error(
'failed to download roots/bedrock package: %s %s',
$ret['stderr'],
$ret['stdout']
);
}

// Create new database
$dbCred = DatabaseGenerator::mysql($this->getAuthContext(), $hostname);
if (!$dbCred->create())
{
if (!$dbCred->create()) {
return false;
}

// Fill in .env file
if (!$this->generateNewConfiguration($hostname, $docroot, $dbCred))
{
if (!$this->generateNewConfiguration($hostname, $docroot, $dbCred)) {
info('removing temporary files');
if (!array_get($opts, 'hold'))
{
if (!array_get($opts, 'hold')) {
$this->file_delete($docroot, true);
$dbCred->rollback();
}
return false;
}

if (!isset($opts['title']))
{
if (!isset($opts['title'])) {
$opts['title'] = 'A Random Blog for a Random Reason';
}

if (!isset($opts['password']))
{
if (!isset($opts['password'])) {
$opts['password'] = Password::generate();
info("autogenerated password `%s'", $opts['password']);
}
Expand All @@ -222,10 +205,8 @@ public function install(string $hostname, string $path = '', array $opts = array
$ret = $this->execCommand($docroot, 'core %(mode)s --admin_email=%(email)s --skip-email ' .
'--url=%(proto)s%(url)s --title=%(title)s --admin_user=%(user)s --exec=%(mysqli81)s ' .
'--admin_password=%(password)s', $args);
if (!$ret['success'])
{
if (!array_get($opts, 'hold'))
{
if (!$ret['success']) {
if (!array_get($opts, 'hold')) {
$dbCred->rollback();
}
return error('failed to create database structure: %s', coalesce($ret['stderr'], $ret['stdout']));
Expand All @@ -235,16 +216,16 @@ public function install(string $hostname, string $path = '', array $opts = array
$wpcli->setConfiguration(['apache_modules' => ['mod_rewrite']]);

$ret = $this->execCommand($docroot, "rewrite structure --hard '/%%postname%%/'");
if (!$ret['success'])
{
if (!$ret['success']) {
return error('failed to set rewrite structure, error: %s', coalesce($ret['stderr'], $ret['stdout']));
}

// Remap public to web dir instead of app dir
if (null === $this->remapPublic($hostname, $path, 'web/'))
{
return error("Failed to remap Bedrock to web/, manually remap from `%s' - Bedrock setup is incomplete!",
$docroot);
if (null === $this->remapPublic($hostname, $path, 'web/')) {
return error(
"Failed to remap Bedrock to web/, manually remap from `%s' - Bedrock setup is incomplete!",
$docroot
);
}

return true;
Expand All @@ -263,17 +244,15 @@ public function get_versions(): array

// Attempt to retrieve cached versions
$cache = \Cache_Super_Global::spawn();
if (false !== ($ver = $cache->get($key)))
{
if (false !== ($ver = $cache->get($key))) {
return $ver;
}

// Retrieve package information for version check
$url = self::VERSION_CHECK_URL;
$context = stream_context_create(['http' => ['timeout' => 5]]);
$contents = file_get_contents($url, false, $context);
if (!$contents)
{
if (!$contents) {
return array();
}
$versions = json_decode($contents, true);
Expand Down Expand Up @@ -305,16 +284,14 @@ public function valid(string $hostname, string $path = ''): bool

public function get_version(string $hostname, string $path = ''): ?string
{
if (!$this->valid($hostname, $path))
{
if (!$this->valid($hostname, $path)) {
return null;
}

$approot = $this->getAppRootPath($hostname, $path);

// is composer.json file missing?
if (!file_exists($approot . '/composer.json'))
{
if (!file_exists($approot . '/composer.json')) {
return null;
}

Expand All @@ -328,8 +305,7 @@ public function get_environment(string $hostname, string $path = ''): ?string
// Read .env value
$ret = $this->execCommand($approotpath, "dotenv get WP_ENV");

if (!$ret['success'])
{
if (!$ret['success']) {
return error('failed to read env: %s', coalesce($ret['stderr'], $ret['stdout']));
}

Expand All @@ -345,8 +321,7 @@ public function set_environment(string $hostname, string $path = '', string $env
'environment' => $environment,
]);

if (!$ret['success'])
{
if (!$ret['success']) {
return error('failed to update env: %s', coalesce($ret['stderr'], $ret['stdout']));
}

Expand All @@ -361,8 +336,7 @@ public function get_environments(string $hostname, string $path = ''): ?array
$approotpath = $this->getAppRootPath($hostname, $path);

// is config/environments/ dir missing?
if (!is_dir($approotpath . '/config/environments/'))
{
if (!is_dir($approotpath . '/config/environments/')) {
return null;
}

Expand Down