Skip to content

Commit

Permalink
Removing part of the instance system to handle all in a single bag
Browse files Browse the repository at this point in the history
  • Loading branch information
woxxy committed Jun 2, 2013
1 parent 75dac4e commit bd2f29f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 73 deletions.
61 changes: 10 additions & 51 deletions classes/Foolz/Package/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,21 @@ public static function destroy($instance = 'default')
* @return \Foolz\Package\Loader The current object
* @throws \DomainException If the directory is not found
*/
public function addDir($dir_name, $dir = null)
public function addDir($dir = null)
{
if ($dir === null)
{
// if $dir is not specified, we use $dir_name as both $dir and $dir_name
$dir = $dir_name;
}

if ( ! is_dir($dir))
{
throw new \DomainException('Directory not found.');
}

$this->dirs[$dir_name] = rtrim($dir,'/').'/';
$this->dirs[] = rtrim($dir,'/').'/';

// set the flag to reload packages on demand
$this->reload = true;

return $this;
}

/**
* Removes a dir from the array of directories to search packages in
* Unsets also all the packages in that directory
*
* @param string $dir_name The named directory
*
* @return \Foolz\Package\Loader
*/
public function removeDir($dir_name)
{
unset($this->dirs[$dir_name]);
unset($this->packages[$dir_name]);
return $this;
}

/**
* Looks for packages in the specified directories and creates the objects
*/
Expand All @@ -144,13 +123,8 @@ public function find()
$this->packages = array();
}

foreach ($this->dirs as $dir_name => $dir)
foreach ($this->dirs as $dir)
{
if ( ! isset($this->packages[$dir_name]))
{
$this->packages[$dir_name] = [];
}

$vendor_paths = $this->findDirs($dir);

foreach ($vendor_paths as $vendor_name => $vendor_path)
Expand All @@ -159,13 +133,12 @@ public function find()

foreach ($package_paths as $package_name => $package_path)
{
if ( ! isset($this->packages[$dir_name][$vendor_name.'/'.$package_name]))
if ( ! isset($this->packages[$vendor_name.'/'.$package_name]))
{
/* @var $package \Foolz\Package\Package */
$package = new $this->type_class($package_path);
$package->setLoader($this);
$package->setDirName($dir_name);
$this->packages[$dir_name][$vendor_name.'/'.$package_name] = $package;
$this->packages[$vendor_name.'/'.$package_name] = $package;
}
}
}
Expand Down Expand Up @@ -206,51 +179,37 @@ protected function findDirs($path)
/**
* Gets all the packages or the packages from the directory
*
* @param null|string $dir_name if specified it gets only a group of packages
*
* @return \Foolz\Package\Package[] All the packages or the packages in the directory
* @throws \OutOfBoundsException If there isn't such a $dir_name set
*/
public function getAll($dir_name = null)
public function getAll()
{
if ($this->reload === true)
{
$this->find();
}

if ($dir_name === null)
{
return $this->packages;
}

if ( ! isset($this->packages[$dir_name]))
{
throw new \OutOfBoundsException('There is no such a directory.');
}

return $this->packages[$dir_name];
return $this->packages;
}

/**
* Gets a single package object
*
* @param string $dir_name The directory name where to find the package
* @param string $slug The slug of the package
*
* @return \Foolz\Package\Package
* @throws \OutOfBoundsException if the package doesn't exist
*/
public function get($dir_name, $slug)
public function get($slug)
{
$packages = $this->getAll();

if ( ! isset($packages[$dir_name][$slug]))
if ( ! isset($packages[$slug]))
{
throw new \OutOfBoundsException('There is no such a package.');
}

$packages[$dir_name][$slug]->setDirName($dir_name);
return $packages[$dir_name][$slug];
return $packages[$slug];
}

/**
Expand Down
25 changes: 3 additions & 22 deletions classes/Foolz/Package/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function __construct($dir)
* Sets a loader to use the relative
*
* @param \Foolz\Package\Loader $loader
* @return \Foolz\Package\Package
*
* @return $this
*/
public function setLoader(\Foolz\Package\Loader $loader)
{
Expand All @@ -100,26 +101,6 @@ public function getLoader()
return $this->loader;
}

/**
* Sets the dir_name used in the loader to get this package
*
* @param string $dir_name
*/
public function setDirName($dir_name)
{
$this->dir_name = $dir_name;
}

/**
* Returns the dir_name used in the loader to get this package
*
* @return string
*/
public function getDirName()
{
return $this->dir_name;
}

/**
* Gets the path to the package
*
Expand Down Expand Up @@ -348,7 +329,7 @@ public function getExtended()

try
{
return $this->getLoader()->get($this->getDirName(), $extended);
return $this->getLoader()->get($extended);
}
catch (\OutOfBoundsException $e)
{
Expand Down

0 comments on commit bd2f29f

Please sign in to comment.