PHP code example of flexibuild / php-safe

1. Go to this page and download the library: Download flexibuild/php-safe library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

flexibuild / php-safe example snippets


return [
    //....
    'components' => [
        //....
        'view' => [
            'renderers' => [
                'sphp' => [
                    'class' => 'flexibuild\phpsafe\ViewRenderer',
                    //the directory or path alias pointing to where Php-Safe engine compiled files will be stored.
                    //'compiledPath' => '@runtime/flexibuild/PhpSafe/compiled',
                    //'fileMode' => null,
                    //'mkDirMode' => 0777,
                    //the name of Yii application cache component for caching rendered files.
                    //'cacheComponent' => 'cache',
                    //'compilerConfig' => [
                        // see flexibuild\phpsafe\Compiler for more info
                    //],
                ],
            ],
        ],
        //....
    ],
    //....
];

    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'generators' => [
            'controller' => 'flexibuild\phpsafe\gii\generators\controller\Generator',
            'crud' => 'flexibuild\phpsafe\gii\generators\crud\Generator',
            'form' => 'flexibuild\phpsafe\gii\generators\form\Generator',
            'module' => 'flexibuild\phpsafe\gii\generators\module\Generator',
        ],
    ];

return [
    //....
    'controllerMap' => [
        //....
        'phpsafe' => 'flexibuild\phpsafe\console\PhpsafeController',
        //....
    ],
    //....
    'components' => [
        //....
        'view' => [
            'renderers' => [
                'sphp' => [
                    'class' => 'flexibuild\phpsafe\ViewRenderer',
                    // ... and other parameters exactly as in the web configuration
                ],
            ],
        ],
        //....
    ],
    //....
];

php composer.phar 

"flexibuild/php-safe": "*"
 echo 'smth' 
 echo \yii\helpers\Html::encode('smth') 
 print 'raw' 
 print 'raw' 
 echo...`) will be converted to safe echo.
If you need to echo raw html you may use `print` syntax.

Notes
-----

 * Php-Safe engine use [`token_get_all()`](http://php.net/manual/en/function.token-get-all.php) method for parsing code. It's pretty fast.

 * Php-Safe engine will not parse functions, classes, interfaces & traits structures.
That also means php-safe engine will not works in body of anonymous functions.

 * Php-Safe engine will parse only code of your `.sphp` view file. It doesn't know anything about your other code.

 * If your php configured to use short tags and/or asp tags, php-safe will also parse this tags.

 * You may configure your IDE to parse `.sphp` files like `.php` files for more convenience.

 * By default php-safe engine will compile views on the first rendering only.

 * Be carefull: code like `<?= print('smth') 
 echo \yii\helpers\Html::encode(print('smth'))