Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-Works committed Oct 25, 2019
1 parent 5d302a6 commit 711e078
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 0 deletions.
13 changes: 13 additions & 0 deletions laravelhelper
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

use LaravelHelper\Commands\SetupIDE;
use Symfony\Component\Console\Application;

$app = new Application('Laravel Helper', '0.1');

$app->add(new SetupIDE());

$app->run();
33 changes: 33 additions & 0 deletions src/Commands/InstallLaravelIDEHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LaravelHelper\Commands;

use LaravelHelper\Processes\BaseProcess;

class InstallLaravelIDEHelper extends BaseProcess
{
/**
* @var array
*/
protected $command = ['composer', 'require', '--dev', 'barryvdh/laravel-ide-helper'];

/**
* @var int
*/
protected $timeOut = 3600;

/**
* @var string
*/
protected $reason = 'maybe network, maybe time is too short';

/**
* @return void
*
* @throws ProcessFailed
*/
public function run()
{
parent::run();
}
}
30 changes: 30 additions & 0 deletions src/Commands/SetupIDE.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace LaravelHelper\Commands;

use LaravelHelper\Processes\LaravelRoot;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SetupIDE extends Command
{

protected function configure()
{
$this->setName('setupIDE')->setDescription('Setup everything for IDE');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
(new LaravelRoot())->run();
(new InstallLaravelIDEHelper())->run();
$output->writeln('done!');
return 0;
}
}
// $io = new SymfonyStyle($input, $output);
// $io->section('Starting to add helper files');
// $io->progressStart(2);
// $io->progressAdvance();
// $io->progressFinish();
18 changes: 18 additions & 0 deletions src/Exceptions/ProcessFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LaravelHelper\Exceptions;

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class ProcessFailed extends ProcessFailedException
{
public function __construct(Process $process, string $reason = 'not sure')
{
parent::__construct($process);
$this->message .= sprintf(
"\n\nThe reason why it failed:\n================\n%s",
$reason
);
}
}
70 changes: 70 additions & 0 deletions src/Processes/BaseProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace LaravelHelper\Processes;

use Exception;
use LaravelHelper\Exceptions\ProcessFailed;
use Symfony\Component\Process\Process;

class BaseProcess
{
/**
* @var array
*/
protected $command = [];

/**
* @var int
*/
protected $timeOut = 60;

/**
* @var string
*/
protected $reason = 'not sure why';

/**
* @return void
*
* @throws ProcessFailed|Exception
*/
protected function run()
{
$this->validateCommand();

$process = $this->runProcess();

if ($this->processFailed($process))
throw new Exception($this->reason, 1);
}

private function processFailed(Process $process): bool
{
return !$process->isSuccessful();
}

private function runProcess(): Process
{
$process = new Process($this->command);

$process->setTimeout($this->timeOut)->run($this->printOutput());

return $process;
}

private function printOutput(): callable
{
return function ($type, $buffer) {
echo $buffer;
};
}

/**
* @throws Exception|void
*/
private function validateCommand()
{
if (empty($this->command))
throw new Exception('You need to pass some command', 1);
}
}
28 changes: 28 additions & 0 deletions src/Processes/LaravelRoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace LaravelHelper\Processes;

use LaravelHelper\Exceptions\ProcessFailed;

class LaravelRoot extends BaseProcess
{
/**
* @var array
*/
protected $command = ['php', 'artisan'];

/**
* @var string
*/
protected $reason = 'It is not the root of Laravel';

/**
* @return void
*
* @throws ProcessFailed
*/
public function run()
{
parent::run();
}
}

0 comments on commit 711e078

Please sign in to comment.