mirror of
https://github.com/php/doc-fr.git
synced 2026-03-23 22:52:18 +01:00
85 lines
2.4 KiB
XML
85 lines
2.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 9c40251a81d8f369c184e83fd142c4cc656a7261 Maintainer: Fan2Shrek Status: ready -->
|
|
<!-- Reviewed: yes -->
|
|
<chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="luasandbox.examples">
|
|
&reftitle.examples;
|
|
<!-- Add one or more of these <section>'s, with one ore more <example>'s -->
|
|
<!-- Make sure each xml:id is unique -->
|
|
<section xml:id="luasandbox.examples-basic">
|
|
<title>Utilisation de base de LuaSandbox</title>
|
|
<simpara>
|
|
Une fois qu'on a compilé PHP avec le support de LuaSandbox, il est possible de commencer à utiliser LuaSandbox pour exécuter en toute sécurité du code Lua fourni par l'utilisateur.
|
|
</simpara>
|
|
<example>
|
|
<title>Exécuter du code Lua</title>
|
|
<programlisting role="php" xml:id="luasandbox.examples.uniqueidhere">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$sandbox = new LuaSandbox;
|
|
$sandbox->setMemoryLimit( 50 * 1024 * 1024 );
|
|
$sandbox->setCPULimit( 10 );
|
|
|
|
// Enregistrer quelques fonctions dans l'environnement Lua
|
|
|
|
function frobnosticate( $v ) {
|
|
return [ $v + 42 ];
|
|
}
|
|
|
|
$sandbox->registerLibrary( 'php', [
|
|
'frobnosticate' => 'frobnosticate',
|
|
'output' => function ( $string ) {
|
|
echo "$string\n";
|
|
},
|
|
'error' => function () {
|
|
throw new LuaSandboxRuntimeError( "Something is wrong" );
|
|
}
|
|
] );
|
|
|
|
// Exécuter du code Lua, y compris des fonctions de rappels dans PHP et dans Lua
|
|
|
|
$luaCode = <<<EOF
|
|
php.output( "Hello, world" );
|
|
|
|
return "Hi", function ( v )
|
|
return php.frobnosticate( v + 200 )
|
|
end
|
|
EOF;
|
|
|
|
list( $hi, $frob ) = $sandbox->loadString( $luaCode )->call();
|
|
assert( $frob->call( 4000 ) === [ 4242 ] );
|
|
|
|
// Les exceptions LuaSandboxRuntimeError lancées par PHP peuvent être capturées dans PHP
|
|
|
|
list( $ok, $message ) = $sandbox->loadString( 'return pcall( php.error )' )->call();
|
|
assert( !$ok );
|
|
assert( $message === 'Something is wrong' );
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</section>
|
|
|
|
</chapter>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|