3 * Psy CodeCleaner to allow PHP super globals.
5 * https://github.com/bobthecow/psysh/issues/353
7 * Copyright © 2017 Justin Hileman <justin@justinhileman.info>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup Maintenance
27 * @author Justin Hileman <justin@justinhileman.info>
31 * Prefix the real command with a bunch of 'global $VAR;' commands, one for each global.
32 * This will make the shell behave as if it was running in the global scope (almost;
33 * variables created in the shell won't become global if no global variable by that name
36 class CodeCleanerGlobalsPass extends \Psy\CodeCleaner\CodeCleanerPass {
37 private static $superglobals = [
38 'GLOBALS', '_SERVER', '_ENV', '_FILES', '_COOKIE', '_POST', '_GET', '_SESSION'
41 public function beforeTraverse( array $nodes ) {
43 foreach ( array_diff( array_keys( $GLOBALS ), self::$superglobals ) as $name ) {
44 array_push( $names, new \PhpParser\Node\Expr\Variable( $name ) );
47 array_unshift( $nodes, new \PhpParser\Node\Stmt\Global_( $names ) );