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 ;
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
*/
2013-04-28 20:32:46 +00:00
class FilesystemRepository extends WritableArrayRepository
2011-09-25 17:59:10 +00:00
{
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
}
2012-11-08 13:35:01 +00:00
try {
$packages = $this -> file -> read ();
2011-10-30 08:09:46 +00:00
2012-11-08 13:35:01 +00:00
if ( ! is_array ( $packages )) {
throw new \UnexpectedValueException ( 'Could not parse package list from the repository' );
}
} catch ( \Exception $e ) {
throw new InvalidRepositoryException ( 'Invalid repository data in ' . $this -> file -> getPath () . ', packages could not be loaded: [' . get_class ( $e ) . '] ' . $e -> getMessage ());
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 ()
{
2013-04-28 11:29:15 +00:00
$data = array ();
2013-04-28 20:32:46 +00:00
$dumper = new ArrayDumper ();
foreach ( $this -> getCanonicalPackages () as $package ) {
$data [] = $dumper -> dump ( $package );
2011-09-25 17:59:10 +00:00
}
2013-04-28 11:29:15 +00:00
$this -> file -> write ( $data );
2011-09-25 17:59:10 +00:00
}
}