Exemple #1
0
 /**
  * creates and receives singleton instance of this class passing parameters to the constructor
  *
  * @error 01702
  * @see Xapp_Autoloader::__construct
  * @param null|string|array $dirs expects a valid value as explained in Xapp_Autoloader::__construct
  * @param null|mixed $options expects instance options
  * @return null|Xapp_Autoloader
  */
 public static function instance($dirs = null, $options = null)
 {
     if (self::$_instance === null) {
         self::$_instance = new self($dirs, $options);
     }
     return self::$_instance;
 }
Exemple #2
0
/**
 * xapp java style dot and wildcard class/package loader imports/requires class or packages from the default xapp base
 * path or paths passed as conf value defined by XAPP_CONF_IMPORT_PATH. the import $parameter can be:
 *
 * 1) absolute file like /var/www/app/foo.php - includes file directly
 * 2) relative file like /app/foo.php - tries to include file from known xapp paths
 * 3) java style class xapp.Xapp.Event - imports class
 * 4) java style package xapp.Xapp.* - imports packages
 *
 * this function can not import:
 * 3) absolute dirs like /var/www/app - not supported!
 * 4) relative dirs like /app - not supported!
 *
 * the function store all imported java style classes and packages into a global package cache to check if package has been
 * already imported and therefore does not execute any import code. the function does not throw any error but simply tries
 * to require the $import param with triggering php error when $import value require fails.
 *
 * The import function does have hidden additional parameters when using java style dotmnotation wildcard loading. the
 * hidden function arguments/parameters are:
 * 1 = either a custom base path from where to load the package or an array with regex exclude rules/patterns
 * 2 = an array with regex exclude rules if 1 is a custom base path. call like:
 *
 * <code>
 *      xapp_import('xapp.Package.*, '/custom/path/');
 *      xapp_import('xapp.Package.*, array('/^regex1$/', '/regex2/i'));
 *      xapp_import('xapp.Package.*, '/custom/path/', array('/^regex1$/', '/regex2/i'));
 * </code>
 *
 * NOTE: the regex syntax for the the path/name exclusion argument must be a valid and complete regex pattern! Also the
 * exclude argument MUST be an array!
 *
 * @param string $import expects a a valid import value as defined above
 * @return boolean
 */
function xapp_import($import)
{
    $path = null;
    //if is php includable file with relative or absolute path with DS separator or . dot separator include directly trying to resolve absolute path
    if (preg_match('/(.*)\\.(php|php5|phps|phtml|inc)$/i', $import, $m)) {
        if (stripos($import, DS) === false) {
            $import = implode(DS, explode('.', trim($m[1], '.* '))) . '.' . trim($m[2], '.* ');
        }
        if (in_array($import, get_required_files())) {
            return;
        }
        if (is_file($import)) {
            require_once $import;
            return true;
        }
        $class = xapp_path(XAPP_PATH_XAPP) . trim($import, DS);
        if (in_array($class, get_required_files())) {
            return;
        }
        if (is_file($class)) {
            require_once $class;
            return true;
        }
        $class = xapp_path(XAPP_PATH_BASE) . trim($import, DS);
        if (in_array($class, get_required_files())) {
            return;
        }
        if (is_file($class)) {
            require_once $class;
            return true;
        }
        $class = xapp_path(XAPP_PATH_ROOT) . trim($import, DS);
        if (in_array($class, get_required_files())) {
            return;
        }
        if (is_file($class)) {
            require_once $class;
            return true;
        }
        require_once $import;
        //is java style import . dot notation with wildcard
    } else {
        $ns = substr($import, 0, strpos($import, '.'));
        $pk = substr(substr($import, strlen($ns) + 1), 0, strpos(substr($import, strlen($ns) + 1), '.'));
        //if ns is not xapp check if composer autoloader and package to import exists
        if (stripos($ns, 'xapp') === false) {
            $ns = substr($import, 0, strpos($import, '.'));
            $pk = substr(substr($import, strlen($ns) + 1), 0, strpos(substr($import, strlen($ns) + 1), '.'));
            //if ns is not xapp check if composer autoloader and package to import exists
            if (stripos($ns, 'xapp') === false) {
                $searchPath = xapp_path(XAPP_PATH_BASE) . 'xapp' . DIRECTORY_SEPARATOR;
                if (!in_array(xapp_path(XAPP_PATH_BASE) . 'autoload.php', get_required_files())) {
                    /**
                     * @Core-Hack
                     */
                    if (is_file($searchPath . 'autoload.php')) {
                        require_once $searchPath . 'autoload.php';
                    } else {
                        trigger_error("unable to include composer vendor autoloader - please make sure composer is installed", E_USER_ERROR);
                    }
                    /*
                    if(is_file(xapp_path(XAPP_PATH_BASE) . 'autoload.php'))
                    {
                    	require_once xapp_path(XAPP_PATH_BASE) . 'autoload.php';
                    }else{
                    	trigger_error("unable to include composer vendor autoloader - please make sure composer is installed", E_USER_ERROR);
                    }
                    */
                }
                if (!is_dir($searchPath . str_replace('.', DS, substr($import, 0, strpos($import, '.', strpos($import, '.') + 1))))) {
                    trigger_error("composer vendor package: {$import} does not exist - please make sure package is installed", E_USER_ERROR);
                }
                return true;
            }
            /*
            if(!in_array(xapp_path(XAPP_PATH_BASE) . 'autoload.php', get_required_files()))
            {
            	if(is_file(xapp_path(XAPP_PATH_BASE) . 'autoload.php'))
            	{
            		require_once xapp_path(XAPP_PATH_BASE) . 'autoload.php';
            	}else{
            		trigger_error("unable to include composer vendor autoloader - please make sure composer is installed", E_USER_ERROR);
            	}
            }
            if(!is_dir(xapp_path(XAPP_PATH_BASE) . str_replace('.', DS, substr($import, 0, strpos($import, '.', (strpos($import, '.') + 1))))))
            {
            	trigger_error("composer vendor package: $import does not exist - please make sure package is installed", E_USER_ERROR);
            }
            return true;
            */
        }
        //if ns is xapp and xapp autoloader is loaded do nothing but only if package is not Ext
        if (stripos($ns, 'xapp') !== false && xapped('Xapp_Autoloader') && Xapp_Autoloader::hasInstance()) {
            if (stripos($pk, 'Ext') === false) {
                return true;
            }
        }
        //init global import array
        if (!isset($GLOBALS['XAPP_IMPORTS'])) {
            $GLOBALS['XAPP_IMPORTS'] = array();
        }
        //if called class or package is already imported do nothing
        if (in_array($import, $GLOBALS['XAPP_IMPORTS'])) {
            return true;
        }
        //import single class
        if (substr($import, -1) !== '*') {
            $base = explode('.', trim($import, '. '));
            $class = array_pop($base);
            if ($base[0] === 'xapp' && SOURCE_SEPARATOR !== '') {
                if (!array_key_exists(1, $base)) {
                    array_push($base, $class, SOURCE_SEPARATOR);
                } else {
                    $base = array_merge(array_slice($base, 0, 2), array(SOURCE_SEPARATOR), array_slice($base, 2));
                }
            }
            $base = implode(DS, $base);
            $path = xapp_path(XAPP_PATH_BASE);
            $path = array_diff(array_merge((array) $path, (array) xapp_conf(XAPP_CONF_IMPORT_PATH)), array(''), array(1));
            foreach ($path as $p) {
                $p = rtrim($p, DS) . DS;
                if (is_file($p . $base . DS . $class . XAPP_EXT)) {
                    array_push($GLOBALS['XAPP_IMPORTS'], $import);
                    require_once $p . $base . DS . $class . XAPP_EXT;
                    return true;
                }
                if (is_file($p . $base . DS . $class . DS . $class . XAPP_EXT)) {
                    array_push($GLOBALS['XAPP_IMPORTS'], $import);
                    require_once $p . $base . DS . $class . DS . $class . XAPP_EXT;
                    return true;
                }
            }
            trigger_error("unable to import: {$import} - class not found", E_USER_ERROR);
            //import package
        } else {
            $regex = null;
            if (func_num_args() >= 2) {
                $args = func_get_args();
                if (array_key_exists(1, $args) && array_key_exists(2, $args) && !empty($args[2])) {
                    $path = (array) $args[1];
                    $regex = (array) $args[2];
                } else {
                    if (is_array($args[1])) {
                        $regex = $args[1];
                    } else {
                        if (strpos($args[1], DS) !== false) {
                            $path = (array) $args[1];
                        }
                    }
                }
            }
            if ($path === null) {
                $path = xapp_path(XAPP_PATH_BASE);
                $path = array_diff(array_merge((array) $path, (array) xapp_conf(XAPP_CONF_IMPORT_PATH)), array(''), array(1));
            }
            foreach ($path as $p) {
                $b = rtrim($p, DS) . DS;
                $p = $b . implode(DS, explode('.', trim($import, '.*'))) . DS;
                $found = false;
                if (($dir = @opendir($p)) !== false) {
                    while (($file = readdir($dir)) !== false) {
                        if ($file === '.' || $file === '..' || stripos($file, '.svn') !== false) {
                            continue;
                        } else {
                            if ($regex !== null) {
                                foreach ($regex as $r) {
                                    if ((bool) preg_match(trim($r), $file)) {
                                        continue;
                                    }
                                }
                            } else {
                                if (is_dir($p . $file)) {
                                    xapp_import(trim($import, '.*') . '.' . $file . '.*', $b, $regex);
                                } else {
                                    if (strpos($file, XAPP_EXT) !== false) {
                                        $i = trim($import, '.*') . '.' . substr($file, 0, strrpos($file, XAPP_EXT));
                                        if (!in_array($i, $GLOBALS['XAPP_IMPORTS'])) {
                                            require_once $p . $file;
                                            array_push($GLOBALS['XAPP_IMPORTS'], $i);
                                            $found = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($found) {
                        break;
                    }
                }
            }
            if (!in_array($import, $GLOBALS['XAPP_IMPORTS'])) {
                array_push($GLOBALS['XAPP_IMPORTS'], $import);
            }
            return true;
        }
    }
    return false;
}