Skip to content

Instantly share code, notes, and snippets.

@jonsherrard
Created January 4, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonsherrard/2ed05f02433c077052e164769bc8da94 to your computer and use it in GitHub Desktop.
Save jonsherrard/2ed05f02433c077052e164769bc8da94 to your computer and use it in GitHub Desktop.
wordpress setup script
<?php
class WordpressSite {
public $base_folder = "/Users/jonsherrard/projects/";
function __construct($project_name) {
$this->location = $this->base_folder.$project_name;
$this->project_name = $project_name;
}
public function line($string) {
printf($string."\n");
}
public function update_gitignore($text) {
file_put_contents($this->location.'/.gitignore', $text.PHP_EOL , FILE_APPEND);
}
public function create_directory() {
$location = $this->location;
chdir($this->base_folder);
file_put_contents("bedrock.zip", file_get_contents("https://github.com/roots/bedrock/archive/master.zip"));
$zip = new ZipArchive;
$file = $this->base_folder."/bedrock.zip";
if ($zip->open($file) === TRUE) {
$zip->extractTo($this->base_folder);
$zip->close();
unlink($file);
rename($this->base_folder."bedrock-master", $location);
self::line("Unzipped Bedrock to dir $location");
} else {
self::line("Unzipping Bedrock Failed");
}
}
function create_database() {
$database_config = array(
'database_name' => $this->generate_config(),
'database_user' => $this->generate_config(),
'database_password' => $this->generate_config()
);
shell_exec("mysql -u root --password=\"\" -e \"CREATE DATABASE ".$database_config['database_name']."; Use ".$database_config['database_name']."; CREATE USER '".$database_config['database_user']."'@'localhost' IDENTIFIED BY '".$database_config['database_password']."';GRANT ALL PRIVILEGES ON * . * TO '".$database_config['database_user']."'@'localhost';\"");
$this->line("Setup database: ".join(" / ", $database_config));
return $database_config;
}
function generate_config() {
$project_name = str_replace('-', '_', $this->project_name);
return $project_name."_".substr(md5(time()), 0, 8);
}
public function create_env() {
$database_config = $this->create_database();
$env_template = file_get_contents($this->location."/.env.example");
$matches = [];
$env_template = str_replace('database_name', $database_config['database_name'], $env_template);
$env_template = str_replace('database_user', $database_config['database_user'], $env_template);
$env_template = str_replace('database_password', $database_config['database_password'], $env_template);
$env_template = str_replace('database_host', "127.0.0.1", $env_template);
$env_template = str_replace('example.com', "$this->project_name.dev", $env_template);
file_put_contents($this->location."/.env", $env_template);
$this->line(".env created successfully");
}
}
$project_name = $argv[1];
$project_title = ucwords(str_replace('-', ' ', $project_name));
$site = New WordpressSite($project_name);
$site->create_directory();
$site->update_gitignore("");
$site->update_gitignore("#Extras");
$site->update_gitignore("web/app/dev");
chdir($site->location);
shell_exec("composer install");
$site->create_env();
shell_exec("git init");
chdir($site->location."/web");
exec("wp core install --title='$project_title' --url='http://$project_name.dev' --admin_user='admin' --admin_password='admin' --admin_email='test@test.com'");
shell_exec("open http://$project_name.dev");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment