Skip to content

Commit

Permalink
Pull env data from config class
Browse files Browse the repository at this point in the history
  • Loading branch information
kilrizzy committed Jul 9, 2017
1 parent 16a202d commit b629a91
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 87 deletions.
39 changes: 8 additions & 31 deletions app/Commands/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands;

use App\Configuration\Config;
use App\FileManagers\HomesteadFileManager;
use App\FileManagers\HostsFileManager;
use App\Support\Vagrant\Vagrant as VagrantSupport;
Expand All @@ -15,13 +16,8 @@ class File extends Command

private $inputInterface;
private $outputInterface;

private $hostPath;
private $homesteadPath;
private $homesteadBoxPath;
private $homesteadAccessDirectoryCommand;
private $file;

private $config;
private $vagrant;

protected function configure()
Expand All @@ -36,10 +32,10 @@ protected function configure()
private function init(InputInterface $input, OutputInterface $output){
$this->inputInterface = $input;
$this->outputInterface = $output;
$this->updateFromConfig();
$vagrantAccessDirectoryCommand = 'cd '.$this->homesteadBoxPath;
if(!empty($this->homesteadAccessDirectoryCommand)){
$vagrantAccessDirectoryCommand = $this->homesteadAccessDirectoryCommand;
$this->config = new Config();
$vagrantAccessDirectoryCommand = 'cd '.$this->config->getHomesteadBoxPath();
if(!empty($this->config->getHomesteadAccessDirectoryCommand())){
$vagrantAccessDirectoryCommand = $this->config->getHomesteadAccessDirectoryCommand();
}
$this->vagrant = new VagrantSupport($vagrantAccessDirectoryCommand);
}
Expand All @@ -52,42 +48,23 @@ private function addCommandArguments(){
);
}

private function updateFromConfig(){
$this->homesteadBoxPath = getenv('HOMESTEAD_BOX_PATH');
$this->homesteadAccessDirectoryCommand = getenv('HOMESTEAD_ACCESS_DIRECTORY_COMMAND');
$this->hostPath = getenv('HOSTS_FILE_PATH');
$this->homesteadPath = getenv('HOMESTEAD_FILE_PATH');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($input, $output);
$this->updateFromOptions();
if($this->file == 'hosts'){
$fileManager = new HostsFileManager($this->hostPath);
$fileManager = new HostsFileManager($this->config->getHostsPath());
$this->outputInterface->writeLn($fileManager->getFileContents());
}
if($this->file == 'homestead'){
$fileManager = new HomesteadFileManager($this->homesteadPath);
$fileManager = new HomesteadFileManager($this->config->getHomesteadPath());
$this->outputInterface->writeLn($fileManager->getFileContents());
}
return;
}

private function updateFromOptions(){
$this->file = $this->inputInterface->getArgument('file');

$this->folder = getenv('LOCAL_SITES_PATH');
$this->folderSuffix = getenv('DEFAULT_FOLDER_SUFFIX');
$this->useComposer = boolval(getenv('USE_COMPOSER'));
$this->composerProject = getenv('DEFAULT_COMPOSER_PROJECT');
$this->hostPath = getenv('HOSTS_FILE_PATH');
$this->hostIP = getenv('HOMESTEAD_HOST_IP');
$this->homesteadPath = getenv('HOMESTEAD_FILE_PATH');
$this->homesteadSitesPath = getenv('HOMESTEAD_SITES_PATH');
$this->homesteadBoxPath = getenv('HOMESTEAD_BOX_PATH');
$this->homesteadAccessDirectoryCommand = getenv('HOMESTEAD_ACCESS_DIRECTORY_COMMAND');
$this->domainExtension = getenv('DEFAULT_DOMAIN_EXTENSION');
}

}
70 changes: 25 additions & 45 deletions app/Commands/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands;

use App\Configuration\Config;
use App\FileManagers\HomesteadFileManager;
use App\FileManagers\HostsFileManager;
use App\Input\Interrogator;
Expand All @@ -18,20 +19,14 @@ class Host extends Command
private $inputInterface;
private $outputInterface;

private $config;

private $name;
private $composerProject;
private $useComposer;
private $folder;
private $folderSuffix;
private $database;
private $domain;
private $domainExtension;
private $hostIP;
private $hostPath;
private $homesteadPath;
private $homesteadSitesPath;
private $homesteadBoxPath;
private $homesteadAccessDirectoryCommand;
private $useDefaults=false;
private $skipConfirmation=false;

Expand All @@ -53,10 +48,10 @@ private function init(InputInterface $input, OutputInterface $output){
$this->outputInterface = $output;
$this->questionHelper = $this->getHelper('question');
$this->interrogator = new Interrogator($input, $output, $this->getHelper('question'));
$this->updateFromConfig();
$vagrantAccessDirectoryCommand = 'cd '.$this->homesteadBoxPath;
if(!empty($this->homesteadAccessDirectoryCommand)){
$vagrantAccessDirectoryCommand = $this->homesteadAccessDirectoryCommand;
$this->config = new Config();
$vagrantAccessDirectoryCommand = 'cd '.$this->config->getHomesteadBoxPath();
if(!empty($this->config->getHomesteadAccessDirectoryCommand())){
$vagrantAccessDirectoryCommand = $this->config->getHomesteadAccessDirectoryCommand();
}
$this->vagrant = new Vagrant($vagrantAccessDirectoryCommand);
}
Expand Down Expand Up @@ -97,20 +92,6 @@ private function addCommandOptions(){
);
}

private function updateFromConfig(){
$this->folder = getenv('LOCAL_SITES_PATH');
$this->folderSuffix = getenv('DEFAULT_FOLDER_SUFFIX');
$this->useComposer = boolval(getenv('USE_COMPOSER'));
$this->composerProject = getenv('DEFAULT_COMPOSER_PROJECT');
$this->hostPath = getenv('HOSTS_FILE_PATH');
$this->hostIP = getenv('HOMESTEAD_HOST_IP');
$this->homesteadPath = getenv('HOMESTEAD_FILE_PATH');
$this->homesteadSitesPath = getenv('HOMESTEAD_SITES_PATH');
$this->homesteadBoxPath = getenv('HOMESTEAD_BOX_PATH');
$this->homesteadAccessDirectoryCommand = getenv('HOMESTEAD_ACCESS_DIRECTORY_COMMAND');
$this->domainExtension = getenv('DEFAULT_DOMAIN_EXTENSION');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($input, $output);
Expand Down Expand Up @@ -165,22 +146,22 @@ private function interrogate(){
);
}

if ($this->useComposer) {
if ($this->config->getUseComposer()) {
$this->composerProject = $this->interrogator->ask(
'What composer project?',
$this->composerProject
$this->config->getComposerProject()
);
}

$this->folder = $this->interrogator->ask(
'What local directory will store your project?',
$this->folder
$this->config->getFolder()
);

if ($this->composerProject != 'laravel/laravel') {
$this->folderSuffix = $this->interrogator->ask(
'Point site to?',
$this->folderSuffix
$this->config->getFolderSuffix()
);
}

Expand All @@ -205,18 +186,17 @@ private function interrogate(){

private function getTaskConfirmationFromQuestion(){
$this->outputInterface->writeln('<info>The following tasks will be executed:</info>');
if($this->useComposer && !empty($this->composerProject)){
if($this->config->getUseComposer() && !empty($this->composerProject)){
$this->outputInterface->writeln("- Run Command: cd {$this->folder} && composer create-project {$this->composerProject} {$this->name}");
}
$this->outputInterface->writeln('- ('.$this->hostPath.') add line: '.$this->hostIP.' '.$this->domain);
$this->outputInterface->writeln('- ('.$this->homesteadPath.') map : '.$this->domain.' to '.$this->homesteadSitesPath.$this->name.$this->folderSuffix);
$this->outputInterface->writeln('- ('.$this->homesteadPath.') add to databases: '.$this->database);
$this->outputInterface->writeln('- ('.$this->config->getHostsPath().') add line: '.$this->config->getHostIP().' '.$this->domain);
$this->outputInterface->writeln('- ('.$this->config->getHomesteadPath().') map : '.$this->domain.' to '.$this->config->getHomesteadSitesPath().$this->name.$this->folderSuffix);
$this->outputInterface->writeln('- ('.$this->config->getHomesteadPath().') add to databases: '.$this->database);
if(!empty($this->homesteadProvisionCommand)){
$this->outputInterface->writeln('- Run Command: '.$this->homesteadProvisionCommand);
}else{
$this->outputInterface->writeln('- Run Command: cd '.$this->homesteadBoxPath.' && vagrant provision');
$this->outputInterface->writeln('- Run Command: cd '.$this->config->getHomesteadBoxPath().' && vagrant provision');
}
$default = 'Y';

$response = $this->interrogator->ask(
'Run tasks?',
Expand All @@ -229,18 +209,18 @@ private function getTaskConfirmationFromQuestion(){
}

private function runTasks(){
if($this->useComposer && !empty($this->composerProject)){
if($this->config->getUseComposer() && !empty($this->composerProject)){
$this->outputInterface->writeln('<info>Creating project...</info>');
$this->createProject();
}

$this->outputInterface->writeln('<info>Adding Domain to hosts file ('.$this->domain.')...</info>');
$this->updateHostsFile();

$this->outputInterface->writeln('<info>Mapping '.$this->domain.' in "'.$this->homesteadPath.'"...</info>');
$this->outputInterface->writeln('<info>Mapping '.$this->domain.' in "'.$this->config->getHomesteadPath().'"...</info>');
$this->updateHomesteadSites();

$this->outputInterface->writeln('<info>Adding database ('.$this->database.') to "'.$this->homesteadPath.'"...</info>');
$this->outputInterface->writeln('<info>Adding database ('.$this->database.') to "'.$this->config->getHomesteadPath().'"...</info>');
$this->updateHomesteadDatabases();

$this->outputInterface->writeln('<info>Provisioning Vagrant...</info>');
Expand All @@ -260,7 +240,7 @@ private function defaultDatabaseNameFromKey($key){
private function defaultDomainNameFromKey($key){
$key = strtolower($key);
$key = preg_replace("/[^A-Za-z0-9]/", '', $key);
$key = $key.$this->domainExtension;
$key = $key.$this->config->getDomainExtension();
return $key;
}

Expand All @@ -270,17 +250,17 @@ private function createProject()
}

private function updateHostsFile(){
$fileManager = new HostsFileManager($this->hostPath);
$fileManager->appendLine($this->hostIP.' '.$this->domain);
$fileManager = new HostsFileManager($this->config->getHostsPath());
$fileManager->appendLine($this->config->getHostIP().' '.$this->domain);
}

private function updateHomesteadSites(){
$fileManager = new HomesteadFileManager($this->homesteadPath);
$fileManager->addMapLineToSites($this->domain, $this->homesteadSitesPath.$this->name.$this->folderSuffix);
$fileManager = new HomesteadFileManager($this->config->getHomesteadPath());
$fileManager->addMapLineToSites($this->domain, $this->config->getHomesteadSitesPath().$this->name.$this->folderSuffix);
}

private function updateHomesteadDatabases(){
$fileManager = new HomesteadFileManager($this->homesteadPath);
$fileManager = new HomesteadFileManager($this->config->getHomesteadPath());
$fileManager->addDatabase($this->database);
}

Expand Down
17 changes: 6 additions & 11 deletions app/Commands/Vagrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands;

use App\Configuration\Config;
use App\Support\Vagrant\Vagrant as VagrantSupport;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -14,11 +15,10 @@ class Vagrant extends Command
private $inputInterface;
private $outputInterface;

private $homesteadBoxPath;
private $homesteadAccessDirectoryCommand;
private $action;

private $vagrant;
private $config;

protected function configure()
{
Expand All @@ -32,10 +32,10 @@ protected function configure()
private function init(InputInterface $input, OutputInterface $output){
$this->inputInterface = $input;
$this->outputInterface = $output;
$this->updateFromConfig();
$vagrantAccessDirectoryCommand = 'cd '.$this->homesteadBoxPath;
if(!empty($this->homesteadAccessDirectoryCommand)){
$vagrantAccessDirectoryCommand = $this->homesteadAccessDirectoryCommand;
$this->config = new Config();
$vagrantAccessDirectoryCommand = 'cd '.$this->config->getHomesteadBoxPath();
if(!empty($this->config->getHomesteadAccessDirectoryCommand())){
$vagrantAccessDirectoryCommand = $this->config->getHomesteadAccessDirectoryCommand();
}
$this->vagrant = new VagrantSupport($vagrantAccessDirectoryCommand);
}
Expand All @@ -48,11 +48,6 @@ private function addCommandArguments(){
);
}

private function updateFromConfig(){
$this->homesteadBoxPath = getenv('HOMESTEAD_BOX_PATH');
$this->homesteadAccessDirectoryCommand = getenv('HOMESTEAD_ACCESS_DIRECTORY_COMMAND');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($input, $output);
Expand Down
82 changes: 82 additions & 0 deletions app/Configuration/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Configuration;

class Config{

private $composerProject;
private $useComposer;
private $folder;
private $folderSuffix;
private $domainExtension;
private $hostIP;
private $hostsPath;
private $homesteadPath;
private $homesteadSitesPath;
private $homesteadBoxPath;
private $homesteadAccessDirectoryCommand;

public function __construct()
{
$this->updateFromEnvironment();
}

private function updateFromEnvironment(){
$this->folder = getenv('LOCAL_SITES_PATH');
$this->folderSuffix = getenv('DEFAULT_FOLDER_SUFFIX');
$this->useComposer = boolval(getenv('USE_COMPOSER'));
$this->composerProject = getenv('DEFAULT_COMPOSER_PROJECT');
$this->hostsPath = getenv('HOSTS_FILE_PATH');
$this->hostIP = getenv('HOMESTEAD_HOST_IP');
$this->homesteadPath = getenv('HOMESTEAD_FILE_PATH');
$this->homesteadSitesPath = getenv('HOMESTEAD_SITES_PATH');
$this->homesteadBoxPath = getenv('HOMESTEAD_BOX_PATH');
$this->homesteadAccessDirectoryCommand = getenv('HOMESTEAD_ACCESS_DIRECTORY_COMMAND');
$this->domainExtension = getenv('DEFAULT_DOMAIN_EXTENSION');
}

public function getFolder(){
return $this->folder;
}

public function getFolderSuffix(){
return $this->folderSuffix;
}

public function getUseComposer(){
return $this->useComposer;
}

public function getComposerProject(){
return $this->composerProject;
}

public function getHostsPath(){
return $this->hostsPath;
}

public function getHostIP(){
return $this->hostIP;
}

public function getHomesteadPath(){
return $this->homesteadPath;
}

public function getHomesteadSitesPath(){
return $this->homesteadSitesPath;
}

public function getHomesteadBoxPath(){
return $this->homesteadBoxPath;
}

public function getHomesteadAccessDirectoryCommand(){
return $this->homesteadAccessDirectoryCommand;
}

public function getDomainExtension(){
return $this->domainExtension;
}

}

0 comments on commit b629a91

Please sign in to comment.