Last commit for lib/browser_runner.php: 9ff742e4cc2ef0dba312dd0c5f642890b6945730

First pass at converting files to use autoloading! Take care if you have an old yioop system you are upgrading, a=chris

Chris Pollett [2015-07-01 02:Jul:st]
First pass at converting files to use autoloading! Take care if you have an old yioop system you are upgrading, a=chris
<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 * Copyright (C) 2009 - 2015  Chris Pollett chris@pollett.org
 * LICENSE:
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * END LICENSE
 *
 * @author Eswara Rajesh Pinapala epinapala@live.com
 * @package seek_quarry
 * @subpackage test
 * @license http://www.gnu.org/licenses/ GPL3
 * @link http://www.seekquarry.com/
 * @copyright 2009 - 2015
 * @filesource
 */
 if(!defined('BASE_DIR')) {echo "BAD REQUEST"; exit();}
/**
 * Used to execute browser-based Javascript and browser page rendering from PHP.
 *
 * @author Eswara Rajesh Pinapala
 * @package seek_quarry
 * @subpackage lib
 */
class BrowserRunner
{
    /**
     * Tests if there is a headless browser (typically Phantom JS) available
     * before constructing this kind of object. If not, it throws an exceptio
     */
    function __construct()
    {
        $version = $this->execute("-v");
        if(!$version){
            throw new Exception("BrowserRunner currently requires PhantomJS ".
                "package to run");
        }
    }
    /**
     *  Runs a Javascript in the current headless browser instance and
     *  return the results as either a JSON or PHP object.
     *  @param string $script Javascript to run in browser
     *  @param string $decode_json whether to leave result as is or to convert
     *      from JSON to a PHP object
     */
    function execute($script, $decode_json = false)
    {
        $shell_result = shell_exec(
            escapeshellcmd(PHANTOM_JS." " . implode(' ', func_get_args())));
        if($shell_result === null) {
            return false;
        }
        if($decode_json) {
            if(substr($shell_result, 0, 1) !== '{') {
                //return if the result is not a JSON.
                return $shell_result;
            } else {
                //If the result is a JSON, decode JSON into a PHP array.
                $json = json_decode($shell_result, true);
                if($json === null) {
                    return false;
                }
                return $json;
            }
        } else {
            return $shell_result;
        }
    }
}
ViewGit