php-pharlite/src/Manifest.php

54 lines
1.2 KiB
PHP

<?php
namespace PharLite;
use Symfony\Component\Finder\Finder;
class Manifest implements \IteratorAggregate, \Countable
{
/** @var string Root directory of the project being built */
protected $root;
/** @var ManifestObject[] The included objects */
protected $objects = [];
public function __construct($root)
{
$this->root = $root;
}
public function addDirectory($path, callable $filter=null)
{
$path = rtrim($path, DIRECTORY_SEPARATOR);
$obj = (new Finder)->files()->in($path);
foreach ($obj as $o) {
if (is_callable($filter) && (false === $filter($o))) {
continue;
}
$path = $o->getPathname();
$this->addFile($path);
}
}
public function addFile($file)
{
if (strpos($file, $this->root) === 0) {
$local = substr($file, 0, strlen($this->root));
} else {
$local = $file;
}
$this->objects[] = new ManifestObject($file, $local);
}
public function getIterator()
{
return new \ArrayIterator($this->objects);
}
public function count()
{
return count($this->objects);
}
}