-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sandboxes and composer installations #152
Open
Markcial
wants to merge
4
commits into
bobthecow:main
Choose a base branch
from
Markcial:sandboxed_composer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
869aa5a
sandboxes and composer installations
Markcial e3dc466
silenced some verbose functions, code cleaning, added more functions …
Markcial dbf3d7c
refactoring of the arguments for the commands, replaced proc_open in …
Markcial 74732a1
removed dependency on symfony process, replaced with proc_open function
Markcial File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
namespace Psy\Command; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ComposerCommand extends Command | ||
{ | ||
const PIPE_WRITE = 0; | ||
const PIPE_READ = 1; | ||
const PIPE_ERR = 2; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $specification = array( | ||
self::PIPE_WRITE => array("pipe", "r"), | ||
self::PIPE_READ => array("pipe", "w"), | ||
self::PIPE_ERR => array("pipe", "w"), | ||
); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $composerPath; | ||
|
||
/** | ||
* @var InputInterface | ||
*/ | ||
protected $input; | ||
|
||
/** | ||
* @var OutputInterface | ||
*/ | ||
protected $output; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('composer') | ||
->setDefinition(array( | ||
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), | ||
)) | ||
->setDescription('Composer installation.') | ||
->setHelp( | ||
<<<HELP | ||
composer library Installs the library | ||
|
||
if composer is not found will install it locally | ||
HELP | ||
); | ||
$this->ignoreValidationErrors(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->input = $input; | ||
$this->output = $output; | ||
|
||
if (!$this->checkComposerInstallation()) { | ||
$this->installComposer(); | ||
} | ||
|
||
$this->callComposerBootstrap(); | ||
|
||
$app = new \Composer\Console\Application(); | ||
$app->setAutoExit(false); | ||
$input = new StringInput(trim(preg_replace(sprintf('#^%s#', $this->getName()), '', $input->__toString()))); | ||
|
||
return $app->doRun($input, $this->output); | ||
} | ||
|
||
/** | ||
* @param $path | ||
*/ | ||
public function setComposerPath($path) | ||
{ | ||
$this->composerPath = $path; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
protected function callComposerBootstrap() | ||
{ | ||
$dependency = sprintf( | ||
'phar://%s/src/bootstrap.php', | ||
!is_null($this->composerPath) ? $this->composerPath : $this->getLocalComposerFile() | ||
); | ||
|
||
require_once $dependency; | ||
} | ||
|
||
/** | ||
* @param $command | ||
* | ||
* @return bool|string | ||
*/ | ||
protected function shellCommand($command) | ||
{ | ||
$process = proc_open($this->getSystemShell(), $this->specification, $pipes); | ||
stream_set_blocking($pipes[self::PIPE_ERR], 0); | ||
|
||
if (is_resource($process)) { | ||
fwrite($pipes[self::PIPE_WRITE], $command); | ||
fclose($pipes[self::PIPE_WRITE]); | ||
|
||
if ($err = stream_get_contents($pipes[self::PIPE_ERR])) { | ||
$err = trim($err); | ||
$this->output->writeln("<error>$err</error>"); | ||
|
||
return false; | ||
} | ||
|
||
$return = stream_get_contents($pipes[self::PIPE_READ]); | ||
fclose($pipes[self::PIPE_READ]); | ||
|
||
if (proc_close($process) === 0) { | ||
return trim($return); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getSystemShell() | ||
{ | ||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | ||
return 'cmd.exe'; | ||
} | ||
|
||
return 'bash'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getLocalComposerFile() | ||
{ | ||
return getcwd() . DIRECTORY_SEPARATOR . 'composer.phar'; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function checkComposerInstallation() | ||
{ | ||
return @file_exists($this->getLocalComposerFile()) or !is_null($this->composerPath); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
protected function installComposer() | ||
{ | ||
$this->output->writeln("<info>Composer not found, downloading.</info>"); | ||
$response = $this->shellCommand('php -r "readfile(\'https://getcomposer.org/installer\');" | php'); | ||
$this->output->writeln("<info>$response</info>"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you use Symfony's Process component instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will check for that, didn't knew that symfony had one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/symfony/Process :)
with this, you do not have to bother anymore with the
proc_open
functions, unix, windows, etcThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, i will check that, maybe today i will update the PR