2017 © Pedro Peláez
 

symfony-bundle phpsandboxbundle

PHP Sandbox Bundle for Symfony 2

image

fracasula/phpsandboxbundle

PHP Sandbox Bundle for Symfony 2

  • Tuesday, February 17, 2015
  • by fracasula
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Getting Started With PhpSandboxBundle

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)

Build Status, (*2)

Prerequisites

This bundle requires Symfony 2.1+., (*3)

Installation

Step 1: Download PhpSandboxBundle using the composer

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)

Step 2: Enable the bundle

Enable the bundle in your kernel:, (*7)

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FraCasula\Bundle\PhpSandboxBundle\FraCasulaPhpSandboxBundle(),
    );
}

Examples

Run PHP Code in the current environment

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

Run PHP Code in a separate sandbox

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();
}

Run PHP Code in background

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
);

The Versions

17/02 2015

dev-master

9999999-dev http://github.com/fracasula/PhpSandboxBundle

PHP Sandbox Bundle for Symfony 2

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Francesco Casula

symfony 2 multitasking php sandbox