php-pharlite/src/Manifest.php

69 lines
1.6 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 = [];
protected $excluded = [];
public function __construct($root)
{
$this->root = $root;
}
public function setExcluded(array $patterns=[])
{
$this->excluded = $patterns;
}
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)
{
// Don't add the file if it matches an excluded pattern
if ($this->excluded) foreach ($this->excluded as $p) {
if (fnmatch($p, $file)) return;
}
// Figure out local path name
if (strpos($file, $this->root) === 0) {
$local = substr($file, 0, strlen($this->root));
} else {
$local = $file;
}
// Add object to manifest
$this->objects[] = new ManifestObject($file, $local);
}
public function getIterator()
{
return new \ArrayIterator($this->objects);
}
public function count()
{
return count($this->objects);
}
}