Exemplo n.º 1
0
 /**
  * Check file from registered path
  *
  * @param null $file
  * @return string
  */
 public function path($file = null)
 {
     $base_path = Composer::getRootPath();
     $pieces = explode('/', $file);
     # Add public to the path
     array_splice($pieces, 2, 0, 'public');
     $vendor_path = implode('/', $pieces);
     if (is_file($base_path . '/workbench/' . $vendor_path)) {
         return $base_path . '/workbench/' . $vendor_path;
     } elseif (is_file($base_path . '/vendor/' . $vendor_path)) {
         return $base_path . '/vendor/' . $vendor_path;
     } else {
         foreach ($this->_paths as $path) {
             $currentPath = $base_path . '/' . $path . '/' . $file;
             if (is_file($currentPath)) {
                 return $currentPath;
             }
         }
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * 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) {
     }
 }