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 #15

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
115 changes: 45 additions & 70 deletions src/lib/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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 @@ -35,19 +34,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 @@ -65,8 +59,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 @@ -97,11 +90,9 @@ protected function generateNewConfiguration(string $domain, string $docroot, Dat
'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($ret['stderr'], $ret['stdout']));
}
}
Expand All @@ -119,46 +110,43 @@ protected function generateNewConfiguration(string $domain, string $docroot, Dat
*/
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']));
}

// Create Bedrock project with specified version
$lock = $this->parseLock($opts['verlock'], $opts['version']);
$ret = $this->execComposer($docroot,
$ret = $this->execComposer(
$docroot,
'create-project --prefer-dist %(package)s %(docroot)s \'%(version)s\'',
[
'package' => static::PACKAGIST_NAME,
Expand All @@ -168,50 +156,47 @@ 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']
);
}

// Remap public to web dir instead of app dir
if (null === ($docroot = $this->remapPublic($hostname, $path, 'web/')))
{
if (null === ($docroot = $this->remapPublic($hostname, $path, 'web/'))) {
$this->file_delete($docroot, true);

return error("Failed to remap Bedrock to web/, manually remap from `%s' - Bedrock setup is incomplete!",
$docroot);
return error(
"Failed to remap Bedrock to web/, manually remap from `%s' - Bedrock setup is incomplete!",
$docroot
);
}

// 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 @@ -233,10 +218,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 @@ -246,8 +229,7 @@ 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']));
}

Expand All @@ -267,17 +249,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 @@ -309,16 +289,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 @@ -333,8 +311,7 @@ public function get_environment(string $hostname, string $path = ''): ?string
// Read .env value
$ret = $this->execCommand($approot, "dotenv get WP_ENV");

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

Expand All @@ -351,8 +328,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 @@ -367,8 +343,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