Skip to content

Instantly share code, notes, and snippets.

@patrickcurl
Created March 11, 2021 22:29
Show Gist options
  • Save patrickcurl/d04a10754048b275182fccd580ac0929 to your computer and use it in GitHub Desktop.
Save patrickcurl/d04a10754048b275182fccd580ac0929 to your computer and use it in GitHub Desktop.
<?php
namespace LaravelCustomInstaller\Installer\Console;
use Laravel\Installer\Console\NewCommand as BaseCommand;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class NewCommand extends BaseCommand
{
/**
* Execute the command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$executionCode = parent::execute($input, $output);
if ($executionCode === 0) {
$name = $input->getArgument('name');
$domain = "{$name}.test";
$email = "admin@{$domain}";
$directory = $name !== '.' ? getcwd() . '/' . $name : '.';
$envFile = "/{$directory}/.env";
$this->replaceInFile('DB_PASSWORD=', 'DB_PASSWORD=test1234', $envFile);
$this->replaceInFile('APP_NAME=Laravel', 'APP_NAME="' . ucwords($name) . '\"', $envFile);
$this->replaceInFile('CACHE_DRIVER=file', 'CACHE_DRIVER=redis', $envFile);
$this->replaceInFile('QUEUE_CONNECTION=file', 'QUEUE_CONNECTION=redis', $envFile);
$this->replaceInFile('SESSION_DRIVER=file', 'SESSION_DRIVER=redis', $envFile);
$this->replaceInFile('MAIL_USERNAME=null', "MAIL_USERNAME={$email}", $envFile);
$this->replaceInFile('MAIL_FROM_ADDRESS=null', "MAIL_FROM_ADDRESS={$email}", $envFile);
$this->replaceInFile('MAIL_PASSWORD=null', 'MAIL_PASSWORD=test1234', $envFile);
$output->writeln(PHP_EOL . '<comment>Overriding successful! Yay!</comment>');
$this->customCommands($directory, $name, $input, $output);
}
return $executionCode;
}
/**
* Create a Git repository and commit the base Laravel skeleton.
*
* @param string $directory
* @param string $name
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function customCommands(string $directory, string $name, InputInterface $input, OutputInterface $output)
{
$home = getenv('HOME');
$symLink = "{$home}/sites/{$name}";
$comments = "";
if (file_exists("{$symLink}")) {
$comments .= PHP_EOL . "<comment>Warning: {$symLink} already exists, overwritten.</comment>";
unlink($symLink);
}
$symlinkCommand = "ln -s {$directory}/public {$symLink}";
$commands = [
"npm i && npm run dev",
$symlinkCommand,
];
chdir($directory);
$this->runCommands($commands, $input, $output);
$output->writeLn($comments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment