Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
lotfio committed Aug 26, 2020
1 parent 25f5c7f commit 842af32
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 105 deletions.
179 changes: 98 additions & 81 deletions src/Conso/Commands/Compile.php
Original file line number Diff line number Diff line change
@@ -1,188 +1,204 @@
<?php namespace Conso\Commands;
<?php

namespace Conso\Commands;

/**
* @author <[email protected]>
* @package Conso PHP Console Creator
*
* @version 2.0.0
*
* @license MIT
*
* @category CLI
*
* @copyright 2019 Lotfio Lakehal
*/

use Conso\Command;
use Conso\Exceptions\{CompileException,InputException};
use Conso\Contracts\{CommandInterface,InputInterface,OutputInterface};
use Conso\Contracts\CommandInterface;
use Conso\Contracts\InputInterface;
use Conso\Contracts\OutputInterface;
use Conso\Exceptions\CompileException;
use Conso\Exceptions\InputException;

class Compile extends Command implements CommandInterface
{
/**
* sub commands
* sub commands.
*
* @var array
*/
protected $sub = [
'init'
'init',
];

/**
* flags
* flags.
*
* @var array
*/
protected $flags = [
'--no-shebang'
'--no-shebang',
];

/**
* command help
* command help.
*
* @var array
*/
protected $help = [
protected $help = [
'sub' => [
'init' => 'initialize .json build file'
'init' => 'initialize .json build file',
],
'flags' => [
'--no-shebang' => 'no stub shebang, usefull when invoking phar from http'
]
'--no-shebang' => 'no stub shebang, usefull when invoking phar from http',
],
];

/**
* command description
* command description.
*
* @var string
*/
protected $description = 'Compile your package to a shareable phar file.';

/**
* set up command dependencies
* set up command dependencies.
*/
public function __construct()
{
$this->cwd = getcwd();
$this->packageFile = $this->cwd . '/conso.json';
$this->packageFile = $this->cwd.'/conso.json';
}

/**
* execute method
* execute method.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output) : void
public function execute(InputInterface $input, OutputInterface $output): void
{
if(!\file_exists($this->packageFile))
if (!\file_exists($this->packageFile)) {
throw new CompileException("package file ($this->packageFile) not found");

if(ini_get('phar.readonly') == 1)
throw new CompileException("phar is read only mode ! it should be turned off from php.init to compile.");

}
if (ini_get('phar.readonly') == 1) {
throw new CompileException('phar is read only mode ! it should be turned off from php.init to compile.');
}
// read package file
$buildFile = (array) json_decode(file_get_contents($this->packageFile));

// validate build file
$this->validateBuildFile($buildFile);

// create a phar file if everything went well
$shebang = ($input->flag('--no-shebang') === false) ? true : false;
if($this->createPhar($buildFile, $shebang))
if ($this->createPhar($buildFile, $shebang)) {
exit($output->writeLn("\npackage compiled successfully .\n\n", 'green'));

}

$output->writeLn("error compiling package.\n", 'red');
}

/**
* init package to compile
* init package to compile.
*
* @return void
*/
public function init(InputInterface $input, OutputInterface $output) : void
public function init(InputInterface $input, OutputInterface $output): void
{
if(!\is_writable($this->cwd))
if (!\is_writable($this->cwd)) {
throw new InputException("{$this->cwd} is not writable.");

if(\file_exists($this->packageFile))
}
if (\file_exists($this->packageFile)) {
throw new InputException("build file ({$this->packageFile}) exists already.");

}
$content = [
"src" => [
"src/Conso",
"vendor"
'src' => [
'src/Conso',
'vendor',
],
"build" => "build",
"stub" => "conso",
"phar" => "conso.phar"
'build' => 'build',
'stub' => 'conso',
'phar' => 'conso.phar',
];

if(\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT)))
if (\file_put_contents($this->packageFile, json_encode($content, JSON_PRETTY_PRINT))) {
exit($output->writeLn("\nbuild file created successfully.\n\n", 'green'));
}

$output->writeLn("error creating build file.\n", 'red');
}

/**
* validate build file
* validate build file.
*
* @param string
*
* @return void
*/
private function validateBuildFile(array $file) : void
private function validateBuildFile(array $file): void
{
if(!is_array($file) || count($file) < 4)
throw new CompileException("build file is not a valid json file.");

if(!in_array('src', array_keys($file)))
throw new CompileException("source (src) directory is missing from package file.");

if(!in_array('build', array_keys($file)))
throw new CompileException("build (build) directory is missing from package file.");

if(!in_array('stub', array_keys($file)))
throw new CompileException("stub (stub) file is missing from package file.");

if(!in_array('phar', array_keys($file)))
throw new CompileException("output (phar) file is missing from package file.");

if(!is_array($file['src']))
throw new CompileException("source (src) directory must be an array of valid directories.");

foreach($file['src'] as $dir)
if(!is_dir($dir))
if (!is_array($file) || count($file) < 4) {
throw new CompileException('build file is not a valid json file.');
}
if (!in_array('src', array_keys($file))) {
throw new CompileException('source (src) directory is missing from package file.');
}
if (!in_array('build', array_keys($file))) {
throw new CompileException('build (build) directory is missing from package file.');
}
if (!in_array('stub', array_keys($file))) {
throw new CompileException('stub (stub) file is missing from package file.');
}
if (!in_array('phar', array_keys($file))) {
throw new CompileException('output (phar) file is missing from package file.');
}
if (!is_array($file['src'])) {
throw new CompileException('source (src) directory must be an array of valid directories.');
}
foreach ($file['src'] as $dir) {
if (!is_dir($dir)) {
throw new CompileException("source ($dir) directory is not a valid directory.");

if(!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build']))
}
}
if (!is_string($file['build']) || !is_dir($file['build']) || !is_writable($file['build'])) {
throw new CompileException("build ({$file['build']}) directory is not a valid directory.");

if(!is_string($file['stub']) || !file_exists($file['stub']))
}
if (!is_string($file['stub']) || !file_exists($file['stub'])) {
throw new CompileException("stub file ({$file['stub']}) not found.");

if(!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false)
}
if (!is_string($file['phar']) || strlen($file['phar']) < 1 || preg_match('/\.phar$/', $file['phar']) == false) {
throw new CompileException("phar file ({$file['phar']}) must be a valid file ends with .phar extension.");
}
}

/**
* create a phar
* create a phar.
*
* @param array $rules
*
* @param array $rules
* @return void
*/
private function createPhar(array $rules, bool $shebang = false) : bool
private function createPhar(array $rules, bool $shebang = false): bool
{
$buildLocation = rtrim($rules['build'], '/') . "/package/";
$buildLocation = rtrim($rules['build'], '/').'/package/';

// delete old build files if any
deleteTree($buildLocation);

// copy project
foreach($rules['src'] as $src)
copyDirectory($src, $buildLocation . rtrim($src, '/'));
foreach ($rules['src'] as $src) {
copyDirectory($src, $buildLocation.rtrim($src, '/'));
}

// copy stub file
copy($rules['stub'], $buildLocation . $rules['stub']);
copy($rules['stub'], $buildLocation.$rules['stub']);

// create phar
$phar = new \Phar(rtrim($rules['build'], '/') . "/" . $rules['phar']);
$phar = new \Phar(rtrim($rules['build'], '/').'/'.$rules['phar']);

// start buffering. Mandatory to modify stub to add shebang
$phar->startBuffering();
Expand All @@ -194,7 +210,7 @@ private function createPhar(array $rules, bool $shebang = false) : bool
$phar->buildFromDirectory($buildLocation);

// add shebang
$stub = "#!/usr/bin/env php \n" . $defaultStub;
$stub = "#!/usr/bin/env php \n".$defaultStub;
$phar->setStub($shebang === true ? $stub : $defaultStub);

// stop buffering
Expand All @@ -203,8 +219,9 @@ private function createPhar(array $rules, bool $shebang = false) : bool
// gzip compressin
$phar->compressFiles(\Phar::GZ);

# Make the file executable
chmod(rtrim($rules['build'], '/') . "/" . $rules['phar'], 0770);
// Make the file executable
chmod(rtrim($rules['build'], '/').'/'.$rules['phar'], 0770);

return deleteTree($buildLocation);
}
}
}
2 changes: 1 addition & 1 deletion src/Conso/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public function setUp(): void
$this->output->disableAnsi();
$this->output->enableTestMode();
}
}
}
35 changes: 19 additions & 16 deletions src/Conso/hlprs.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,46 +117,49 @@ function commandHelp(array $command, $output)
}

/**
* copy directory recursively
* copy directory recursively.
*
* @param string $source
* @param string $destination
*
* @return void
*/
function copyDirectory(string $source, string $destination) : void
function copyDirectory(string $source, string $destination): void
{
mkdir($destination, 0755, true);

foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item
)
{
\RecursiveIteratorIterator::SELF_FIRST
) as $item
) {
if ($item->isDir()) {
mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
mkdir($destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
} else {
copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
copy($item, $destination.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
}
}
}

/**
* delete directory recursively
* delete directory recursively.
*
* @param string $dir
*
* @return void
*/
function deleteTree(string $dir)
{
if(!is_dir($dir))
{
if (!is_dir($dir)) {
return false;

$files = array_diff(scandir($dir), array('.', '..'));
}

$files = array_diff(scandir($dir), ['.', '..']);

foreach ($files as $file) {
(is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file");
foreach ($files as $file) {
(is_dir("$dir/$file")) ? deleteTree("$dir/$file") : unlink("$dir/$file");
}

return rmdir($dir);
}
return rmdir($dir);
}
5 changes: 2 additions & 3 deletions tests/Unit/CommandInvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* @copyright 2019 Lotfio Lakehal
*/

use Conso\CommandInvoker;
use Conso\Conso;
use Conso\Input;
use Conso\Output;
use Conso\CommandInvoker;
use Conso\Testing\TestCase;

class CommandInvokerTest extends TestCase
Expand All @@ -37,7 +36,7 @@ class CommandInvokerTest extends TestCase
public function setUp(): void
{
parent::setUp();

$this->commands = [
[
'name' => 'make',
Expand Down
Loading

0 comments on commit 842af32

Please sign in to comment.