2011-09-25 17:59:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Repository;
|
|
|
|
|
2011-10-01 13:01:33 +00:00
|
|
|
use Composer\Json\JsonFile;
|
2012-04-27 17:41:53 +00:00
|
|
|
use Composer\Package\AliasPackage;
|
2011-09-25 17:59:10 +00:00
|
|
|
use Composer\Package\Loader\ArrayLoader;
|
|
|
|
use Composer\Package\Dumper\ArrayDumper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filesystem repository.
|
|
|
|
*
|
|
|
|
* @author Konstantin Kudryashov <ever.zet@gmail.com>
|
2012-04-15 17:05:16 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-09-25 17:59:10 +00:00
|
|
|
*/
|
|
|
|
class FilesystemRepository extends ArrayRepository implements WritableRepositoryInterface
|
|
|
|
{
|
|
|
|
private $file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes filesystem repository.
|
|
|
|
*
|
2012-05-22 10:07:08 +00:00
|
|
|
* @param JsonFile $repositoryFile repository json file
|
2011-09-25 17:59:10 +00:00
|
|
|
*/
|
2011-10-01 13:01:33 +00:00
|
|
|
public function __construct(JsonFile $repositoryFile)
|
2011-09-25 17:59:10 +00:00
|
|
|
{
|
|
|
|
$this->file = $repositoryFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes repository (reads file, or remote address).
|
|
|
|
*/
|
|
|
|
protected function initialize()
|
|
|
|
{
|
|
|
|
parent::initialize();
|
|
|
|
|
2011-10-30 08:09:46 +00:00
|
|
|
if (!$this->file->exists()) {
|
|
|
|
return;
|
2011-10-01 14:06:14 +00:00
|
|
|
}
|
|
|
|
|
2011-10-30 08:09:46 +00:00
|
|
|
$packages = $this->file->read();
|
|
|
|
|
2011-10-29 05:46:05 +00:00
|
|
|
if (!is_array($packages)) {
|
2011-10-30 08:09:46 +00:00
|
|
|
throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository');
|
2011-10-29 05:46:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-12 20:46:21 +00:00
|
|
|
$loader = new ArrayLoader();
|
2012-02-25 01:52:19 +00:00
|
|
|
foreach ($packages as $packageData) {
|
|
|
|
$package = $loader->load($packageData);
|
2012-04-27 17:41:53 +00:00
|
|
|
$this->addPackage($package);
|
|
|
|
}
|
2011-09-25 17:59:10 +00:00
|
|
|
}
|
|
|
|
|
2012-04-15 17:05:16 +00:00
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$this->packages = null;
|
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
|
2011-09-25 17:59:10 +00:00
|
|
|
/**
|
|
|
|
* Writes writable repository.
|
|
|
|
*/
|
|
|
|
public function write()
|
|
|
|
{
|
|
|
|
$packages = array();
|
|
|
|
$dumper = new ArrayDumper();
|
|
|
|
foreach ($this->getPackages() as $package) {
|
2012-04-27 17:41:53 +00:00
|
|
|
if (!$package instanceof AliasPackage) {
|
|
|
|
$data = $dumper->dump($package);
|
|
|
|
$packages[] = $data;
|
2012-02-25 01:52:19 +00:00
|
|
|
}
|
2011-09-25 17:59:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 13:01:33 +00:00
|
|
|
$this->file->write($packages);
|
2011-09-25 17:59:10 +00:00
|
|
|
}
|
|
|
|
}
|