From 361792dbd6b4182d88dbb740f7f1313b252a2bd7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 23 Jan 2015 17:30:30 +0100 Subject: [PATCH] initial sensiolabs/melody integration --- .gitignore | 1 + README.mdown | 1 + composer.json | 3 ++- index.php | 21 +++++++++++++--- lib/MelodyPlugin.php | 59 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 lib/MelodyPlugin.php diff --git a/.gitignore b/.gitignore index 7aa99c1..267264b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor config.php +composer.lock diff --git a/README.mdown b/README.mdown index 215171b..9c7d7a3 100644 --- a/README.mdown +++ b/README.mdown @@ -48,6 +48,7 @@ Changelog --------- - 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 - Layout is now flex-css based - Added a new `bootstrap` option to be include before source evaluation diff --git a/composer.json b/composer.json index 703b60e..6a6c06b 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=5.2.0" + "php": ">=5.2.0", + "sensiolabs/melody": "@dev" }, "extra": { "branch-alias": { diff --git a/index.php b/index.php index 46c789d..9f3a838 100644 --- a/index.php +++ b/index.php @@ -43,6 +43,8 @@ if (!in_array('*', $options['ip_whitelist'], true) && define('PHP_CONSOLE_VERSION', '1.4.0'); require 'krumo/class.krumo.php'; +require 'lib/MelodyPlugin.php'; +require 'vendor/autoload.php'; ini_set('log_errors', 0); ini_set('display_errors', 1); @@ -63,8 +65,7 @@ if (isset($_POST['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 = preg_replace('{^\s*<\?(php)?\s*}i', '', $_POST['code']); + $code = $_POST['code']; // 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])) { @@ -86,7 +87,19 @@ if (isset($_POST['code'])) { $memBefore = memory_get_usage(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 $end = microtime(true); @@ -190,4 +203,4 @@ if (isset($_POST['code'])) { - + \ No newline at end of file diff --git a/lib/MelodyPlugin.php b/lib/MelodyPlugin.php new file mode 100644 index 0000000..f66fca4 --- /dev/null +++ b/lib/MelodyPlugin.php @@ -0,0 +1,59 @@ +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); + } +} \ No newline at end of file