/** * Autoload an undefined class and add more resolving case for the workbench environment * * Example : if in your workbench package you call a class with xxxController, xxxModel, xxxClass at the end, it * will try to resolve the class by searching inside the controllers folder * * /!\ But you must always add a classmap in your composer.json file for better performance ! * * @param $className * @param array $aParam * * @return void * */ public static function autoload($className, $aParam = array()) { $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; $composerName = ''; $supposedPath = null; if ($lastNsPos = strrpos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; $aExplode = explode('\\', $namespace); $iExplode = count($aExplode); if ($iExplode === 1) { $composerName = $packageName = $aExplode[0]; } elseif ($iExplode === 2) { list($vendorName, $packageName) = $aExplode; $composerName = $vendorName . '/' . $packageName; } elseif ($iExplode >= 3) { $vendorName = array_shift($aExplode); $packageName = array_shift($aExplode); $composerName = $vendorName . '/' . $packageName; } } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; $aNamespaces = Arx\classes\Composer::getNamespaces(); foreach ($aNamespaces as $key => $paths) { if (substr($key, -1) == '\\') { $key = substr($key, 0, -1); } foreach ($paths as $i => $path) { $aNamespaces[$key][$i] = $path . DS . str_replace('\\', DS, $namespace); } } $aNamespacesPSR4 = Arx\classes\Composer::getNamespacesPSR4(); foreach ($aNamespacesPSR4 as $key => $paths) { if (substr($key, -1) == '\\') { $key = substr($key, 0, -1); } foreach ($paths as $i => $path) { $aNamespaces[$key][$i] = $path; } } if (in_array($namespace, array_keys($aNamespaces))) { } elseif (in_array($composerName, array_keys($aNamespaces))) { $paths = $aNamespaces[$composerName]; foreach ($paths as $path) { if (is_file($fileName = $path . '/' . $fileName)) { include $fileName; } } } if (isset($aNamespaces[$composerName]) && !empty($aNamespaces[$composerName])) { if (preg_match('/Controller$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'controllers' . DS . $className . '.php'; } elseif (preg_match('/Model$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'models' . DS . $className . '.php'; } elseif (preg_match('/Class$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'classes' . DS . $className . '.php'; } elseif (preg_match('/Command$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'commands' . DS . $className . '.php'; } elseif (preg_match('/Provider$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'providers' . DS . $className . '.php'; } elseif (preg_match('/Facade$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'facades' . DS . $className . '.php'; } elseif (preg_match('/Helper$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'helpers' . DS . $className . '.php'; } elseif (preg_match('/Interface$/', $className)) { $supposedPath = end($aNamespaces[$composerName]) . DS . 'interfaces' . DS . $className . '.php'; } } $pathsWorkbench = Composer::getRootPath('workbench'); if (class_exists('Config', false)) { $pathsWorkbench = Config::get('paths.workbench'); } try { if (is_file($fileName = $pathsWorkbench . DS . strtolower($composerName) . DS . 'src' . DS . $fileName)) { include $fileName; } elseif (is_file($fileName = $pathsWorkbench . DS . $fileName)) { include $fileName; } elseif (is_file($supposedPath)) { include $supposedPath; } elseif (isset($aNamespaces[$composerName]) && is_file(end($aNamespaces[$composerName]) . DS . 'models' . DS . $className . '.php')) { include end($aNamespaces[$composerName]) . DS . 'models' . DS . $className . '.php'; } } catch (Exception $e) { } }
/** * Bootstrap define how the app should include his configuration. By default, it includes file from ../bootstraps folder as Laravel do * * You can also override some arx configs too for a special case * * @see ../bootstraps/default.php * @see ../config/ * * @param string $file * * @param string $config * @throws Exception */ public function bootstrap($file = 'start.php', $config = null) { global $app; $app = $this; if ($config) { $this['arxconfig'] = Config::load($config); } else { // Inject special ARX Config to APP $this['arxconfig'] = Config::load(__DIR__ . '/../config/'); } if (is_file($file)) { require_once $file; } elseif (is_file($file = __DIR__ . '/../../bootstrap/' . $file)) { require_once $file; } else { throw new Exception('Whoops, there is no file to boot...'); } }
/** * List directory content * * @access public * * @param array $exclude * @param bool $recursive * * @return array */ public function scan($c = true, &$list = array(), $exclude_extension = array(), $exclude_file = array(), $exclude_dir = array(), $dir = "") { if (is_array($c)) { foreach ($c as $key => $item) { ${$key} = $item; } } else { $recursive = $c; } if (!$exclude_extension) { $exclude_extension = $this->_exclude_extension; } if (!$exclude_file) { $exclude_file = $this->_exclude_file; } if (!$exclude_dir) { $exclude_dir = $this->_exclude_dir; } // Lowercase exclude Arr $exclude_extension = array_map("strtolower", $exclude_extension); $exclude_file = array_map("strtolower", $exclude_file); $exclude_dir = array_map("strtolower", $exclude_dir); $dir = $dir == "" ? $this->_path : $dir; if (substr($dir, -1) != DS) { $dir .= DS; } // Open the folder $dir_handle = @opendir($dir) or die("Unable to open {$dir}"); // Loop through the files while ($file = readdir($dir_handle)) { // Strip dir pointers and extension exclude $extension = $this->getExtension($file); if ($file == "." || $file == ".." || in_array($extension, $exclude_extension)) { continue; } if (is_dir($dir . $file)) { if (!in_array(strtolower($file), $exclude_dir)) { $info = ""; $info["type"] = "dir"; $info["path"] = $dir; $info["fullpath"] = $dir . $file; $info["urlpath"] = str_replace(Config::get('paths.root'), Config::get('paths.rooturl'), $info["fullpath"]); $info["name"] = str_replace($dir, '', $info["fullpath"]); $list[] = $info; } } else { if (!in_array(strtolower($file), $exclude_file)) { $info = ""; $info["extension"] = $extension; $info["type"] = "file"; $info["path"] = $dir; $info["filename"] = $file; $info["fullpath"] = $dir . $file; $info["urlpath"] = str_replace(Config::get('paths.root'), Config::get('paths.rooturl'), $info["fullpath"]); $info["name"] = str_replace('.' . $extension, '', $file); $list[] = $info; } } if ($recursive && is_dir($dir . $file) && !in_array(strtolower($file), $exclude_dir)) { $this->scan($recursive, $list, $exclude_extension, $exclude_file, $exclude_dir, $dir . $file); } } // Close closedir($dir_handle); return $list; }