diff --git a/src/Loader.php b/src/Loader.php index 8a7543a..175799b 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -103,10 +103,7 @@ public function addDir($dir = null) throw new \DomainException('Directory not found.'); } - $this->dirs[] = rtrim($dir, '/'); - - // set the flag to reload packages on demand - $this->reload = true; + $this->dirs[] = ['path' => rtrim($dir, '/'), 'loaded' => false]; return $this; } @@ -120,21 +117,24 @@ public function find() $this->packages = array(); } - foreach ($this->dirs as $dir) { - $vendor_paths = $this->findDirs($dir); + foreach ($this->dirs as $pack => $dir) { + if ($dir['loaded'] === false) { + $vendors = $this->findDirs($dir['path']); - foreach ($vendor_paths as $vendor_name => $vendor_path) { - $package_paths = $this->findDirs($vendor_path); + foreach ($vendors as $vendor_name => $vendor_path) { + $packages = $this->findDirs($vendor_path); - foreach ($package_paths as $package_name => $package_path) { - if (!isset($this->packages[$vendor_name.'/'.$package_name])) { - /* @var $package \Foolz\Package\Package */ - $package = new $this->type_class($package_path); - $package->setLoader($this); + foreach ($packages as $package_name => $package_path) { + if (!isset($this->packages[$vendor_name.'/'.$package_name])) { + $package = new $this->type_class($package_path); + $package->setLoader($this); - $this->packages[$vendor_name.'/'.$package_name] = $package; + $this->packages[$vendor_name.'/'.$package_name] = $package; + } } } + + $this->dirs[$pack]['loaded'] = true; } } } @@ -175,9 +175,7 @@ protected function findDirs($path) */ public function getAll() { - if ($this->reload === true) { - $this->find(); - } + $this->find(); return $this->packages; } @@ -258,4 +256,18 @@ public function getBaseUrl() return $this->base_url; } + + /** + * @return \Foolz\Theme\Loader + */ + public function reload() + { + foreach ($this->dirs as $pack => $dir) { + $this->dirs[$pack]['loaded'] = false; + } + + $this->packages = array(); + + return $this; + } } diff --git a/src/Package.php b/src/Package.php index 5fea0f0..df2d908 100644 --- a/src/Package.php +++ b/src/Package.php @@ -69,7 +69,8 @@ class Package */ public function __construct($dir) { - $dir = rtrim($dir,'/').'/'; + $dir = rtrim($dir, '/').'/'; + if (!file_exists($dir.'composer.json')) { throw new \DomainException('Directory not found.'); }