dev-master
9999999-dev http://github.com/fracasula/PhpSandboxBundlePHP Sandbox Bundle for Symfony 2
MIT
The Requires
- php >=5.4
The Development Requires
by Francesco Casula
symfony 2 multitasking php sandbox
PHP Sandbox Bundle for Symfony 2
With this bundle you can run PHP Code in a sandbox or in your current environment. Otherwise you can use it for multi-tasking purposes running child processes in background., (*1)
This bundle requires Symfony 2.1+., (*3)
Add PhpSandboxBundle in your composer.json:, (*4)
"require": { "fracasula/phpsandboxbundle": "dev-master" }
Now tell composer to download the bundle by running the command:, (*5)
$ php composer.phar update fracasula/phpsandboxbundle
Composer will install the bundle to your project vendor/fracasula
directory., (*6)
Enable the bundle in your kernel:, (*7)
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FraCasula\Bundle\PhpSandboxBundle\FraCasulaPhpSandboxBundle(), ); }
Note: sharing functions, classes and propagating errors/exceptions (like eval), (*8)
<?php class Test { public $x; } // ... inside a controller $sandbox = $this->container->get('fra_casula_php_sandbox'); $result = $sandbox->run('$test = new Test(); $text->x = 5; echo $test->x;'); echo $result; // will output 5 // or... $result = $sandbox->run('echo intval($_SANDBOX["arg1"]) * 2;', array('arg1' => '10')); echo $result; // 20
Note: without class/functions sharing and without errors propagating, (*9)
The code is executed in a separated process, (*10)
<?php $variables = array('arg1' => '3'); $result = $sandbox->runStandalone('echo intval($_SERVER["arg1"]) * 2;', $variables); echo $result; // 6
Another example:, (*11)
<?php use FraCasula\Bundle\PhpSandboxBundle\Exception\PhpSandboxNotice; use FraCasula\Bundle\PhpSandboxBundle\Exception\PhpSandboxWarning; use FraCasula\Bundle\PhpSandboxBundle\Exception\PhpSandboxError; // ... try { $sandbox->runStandalone('$arr = array(1, 2, 3); echo $arr[100];'); } catch (PhpSandboxNotice $e) { // this will print: // [NOTICE OCCURRED] PHP Notice: Undefined offset: 100 in - on line 1 echo '[NOTICE OCCURRED] ' . $e->getMessage(); }
Note: process forking, so without class/functions sharing and without errors propagating, (*12)
The code is executed in a separated child process, (*13)
$sandbox->runInBackground ( 'imagecopyresized(/* ... */)', array('arg1', 'arg2'), true // TRUE means "wait for child response" | FALSE don't wait );
PHP Sandbox Bundle for Symfony 2
MIT
symfony 2 multitasking php sandbox