initial sensiolabs/melody integration

master
Markus Staab 10 years ago
parent ef78c12c12
commit 361792dbd6
  1. 1
      .gitignore
  2. 1
      README.mdown
  3. 3
      composer.json
  4. 19
      index.php
  5. 59
      lib/MelodyPlugin.php

1
.gitignore vendored

@ -1,2 +1,3 @@
vendor vendor
config.php config.php
composer.lock

@ -48,6 +48,7 @@ Changelog
--------- ---------
- 1.5.0-dev - 1.5.0-dev
- Added melody-script integration. requires a composer binary within the systems/webservers PATH env variable.
- Updated bundled ACE editor to 1.1.8 - Updated bundled ACE editor to 1.1.8
- Layout is now flex-css based - Layout is now flex-css based
- Added a new `bootstrap` option to be include before source evaluation - Added a new `bootstrap` option to be include before source evaluation

@ -13,7 +13,8 @@
} }
], ],
"require": { "require": {
"php": ">=5.2.0" "php": ">=5.2.0",
"sensiolabs/melody": "@dev"
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {

@ -43,6 +43,8 @@ if (!in_array('*', $options['ip_whitelist'], true) &&
define('PHP_CONSOLE_VERSION', '1.4.0'); define('PHP_CONSOLE_VERSION', '1.4.0');
require 'krumo/class.krumo.php'; require 'krumo/class.krumo.php';
require 'lib/MelodyPlugin.php';
require 'vendor/autoload.php';
ini_set('log_errors', 0); ini_set('log_errors', 0);
ini_set('display_errors', 1); ini_set('display_errors', 1);
@ -63,8 +65,7 @@ if (isset($_POST['code'])) {
$code = stripslashes($code); $code = stripslashes($code);
} }
// Important: replace only line by line, so the generated source lines will map 1:1 to the initial user input! $code = $_POST['code'];
$code = preg_replace('{^\s*<\?(php)?\s*}i', '', $_POST['code']);
// if there's only one line wrap it into a krumo() call // if there's only one line wrap it into a krumo() call
if (preg_match('#^(?!var_dump|echo|print|< )([^\r\n]+?);?\s*$#is', $code, $m) && trim($m[1])) { if (preg_match('#^(?!var_dump|echo|print|< )([^\r\n]+?);?\s*$#is', $code, $m) && trim($m[1])) {
@ -86,7 +87,19 @@ if (isset($_POST['code'])) {
$memBefore = memory_get_usage(true); $memBefore = memory_get_usage(true);
$start = microtime(true); $start = microtime(true);
runCode($code, $options['bootstrap']); $melodyPlugin = new MelodyPlugin();
if ($melodyPlugin->isMelodyScript($code)) {
if ($melodyPlugin->isScriptingSupported()) {
$melodyPlugin->runScript($code, $options['bootstrap']);
} else {
throw new Exception('php-console misses required dependencies to run melody scripts.');
}
} else {
// Important: replace only line by line, so the generated source lines will map 1:1 to the initial user input!
$code = preg_replace('{^\s*<\?(php)?\s*}i', '', $code);
runCode($code, $options['bootstrap']);
}
// compare with peak, because regular memory could be free'd already // compare with peak, because regular memory could be free'd already
$end = microtime(true); $end = microtime(true);

@ -0,0 +1,59 @@
<?php
use SensioLabs\Melody\Melody;
use SensioLabs\Melody\Configuration\RunConfiguration;
use Symfony\Component\Process\Process;
use SensioLabs\Melody\Resource\ResourceParser;
/**
* Class which integrates melody scripts into the php-console.
*
* @author mstaab
* @see https://github.com/sensiolabs/melody
*/
class MelodyPlugin {
public function isMelodyScript($source) {
return preg_match(ResourceParser::MELODY_PATTERN, $source);
}
public function isScriptingSupported() {
// the melody lib is bundled with the console, so the only additional requirement is a composer CLI
exec('which composer', $out, $ret);
return $ret === 0;
}
public function runScript($__source_code, $__bootstrap_file)
{
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'melody-composer';
// make sure the melody subprocess has a composer home,
// which is not the case when running from webcontext
$_ENV['COMPOSER_HOME'] = $tmpDir;
$melody = new Melody();
$configuration = new RunConfiguration(/*true, true*/);
$executor = function (Process $process, $verbose)
{
$callback = function ($type, $text)
{
// we only have one output channel to the browser, just echo "all the things"
echo $text;
};
$process->run($callback);
};
//TODO missing $__bootstrap_file support
/*
if ($__bootstrap_file) {
require $__bootstrap_file;
}
*/
$tmpFile = tempnam($tmpDir, '_script');
register_shutdown_function(function() use ($tmpFile) {
@unlink($tmpFile);
});
file_put_contents($tmpFile, $__source_code);
$melody->run($tmpFile, array(), $configuration, $executor);
}
}
Loading…
Cancel
Save