123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- namespace PHPExiftool;
- use Doctrine\Common\Collections\ArrayCollection;
- use PHPExiftool\Exception\EmptyCollectionException;
- use PHPExiftool\Exception\LogicException;
- use PHPExiftool\Exception\RuntimeException;
- use Psr\Log\LoggerInterface;
- class Reader implements \IteratorAggregate
- {
- protected $files = array();
- protected $dirs = array();
- protected $excludeDirs = array();
- protected $extensions = array();
- protected $extensionsToggle = null;
- protected $followSymLinks = false;
- protected $recursive = true;
- protected $ignoreDotFile = false;
- protected $sort = array();
- protected $parser;
- protected $exiftool;
- protected $timeout = 60;
-
- protected $collection;
- protected $readers = array();
-
- public function __construct(Exiftool $exiftool, RDFParser $parser)
- {
- $this->exiftool = $exiftool;
- $this->parser = $parser;
- }
- public function __destruct()
- {
- $this->parser = null;
- $this->collection = null;
- }
- public function setTimeout($timeout)
- {
- $this->timeout = $timeout;
- return $this;
- }
- public function reset()
- {
- $this->files
- = $this->dirs
- = $this->excludeDirs
- = $this->extensions
- = $this->sort
- = $this->readers = array();
- $this->recursive = true;
- $this->ignoreDotFile = $this->followSymLinks = false;
- $this->extensionsToggle = null;
- return $this;
- }
-
- public function getIterator()
- {
- return $this->all()->getIterator();
- }
-
- public function files($files)
- {
- $this->resetResults();
- $this->files = array_merge($this->files, (array) $files);
- return $this;
- }
-
- public function in($dirs)
- {
- $this->resetResults();
- $this->dirs = array_merge($this->dirs, (array) $dirs);
- return $this;
- }
-
- public function append(Reader $reader)
- {
- $this->resetResults();
- $this->readers[] = $reader;
- return $this;
- }
-
- public function sort($by)
- {
- static $availableSorts = array(
- 'directory', 'filename', 'createdate', 'modifydate', 'filesize'
- );
- foreach ((array) $by as $sort) {
- if ( ! in_array($sort, $availableSorts)) {
- continue;
- }
- $this->sort[] = $sort;
- }
- return $this;
- }
-
- public function exclude($dirs)
- {
- $this->resetResults();
- $this->excludeDirs = array_merge($this->excludeDirs, (array) $dirs);
- return $this;
- }
-
- public function extensions($extensions, $restrict = true)
- {
- $this->resetResults();
- if ( ! is_null($this->extensionsToggle)) {
- if ((boolean) $restrict !== $this->extensionsToggle) {
- throw new LogicException('You cannot restrict extensions AND exclude extension at the same time');
- }
- }
- $this->extensionsToggle = (boolean) $restrict;
- $this->extensions = array_merge($this->extensions, (array) $extensions);
- return $this;
- }
-
- public function followSymLinks()
- {
- $this->resetResults();
- $this->followSymLinks = true;
- return $this;
- }
-
- public function ignoreDotFiles()
- {
- $this->resetResults();
- $this->ignoreDotFile = true;
- return $this;
- }
-
- public function notRecursive()
- {
- $this->resetResults();
- $this->recursive = false;
- return $this;
- }
-
- public function getOneOrNull()
- {
- return count($this->all()) === 0 ? null : $this->all()->first();
- }
-
- public function first()
- {
- if (count($this->all()) === 0) {
- throw new EmptyCollectionException('Collection is empty');
- }
- return $this->all()->first();
- }
-
- public function all()
- {
- if (! $this->collection) {
- $this->collection = $this->buildQueryAndExecute();
- }
- if ($this->readers) {
- $elements = $this->collection->toArray();
- $this->collection = null;
- foreach ($this->readers as $reader) {
- $elements = array_merge($elements, $reader->all()->toArray());
- }
- $this->collection = new ArrayCollection($elements);
- }
- return $this->collection;
- }
- public static function create(LoggerInterface $logger)
- {
- return new static(new Exiftool($logger), new RDFParser());
- }
-
- protected function resetResults()
- {
- $this->collection = null;
- return $this;
- }
-
- protected function buildQueryAndExecute()
- {
- $result = '';
- try {
- $result = trim($this->exiftool->executeCommand($this->buildQuery(), $this->timeout));
- } catch (RuntimeException $e) {
-
- if (! $this->ignoreDotFile) {
- throw $e;
- }
- }
- if ($result === '') {
- return new ArrayCollection();
- }
- $this->parser->open($result);
- return $this->parser->ParseEntities();
- }
-
- protected function computeExcludeDirs($rawExcludeDirs, $rawSearchDirs)
- {
- $excludeDirs = array();
- foreach ($rawExcludeDirs as $excludeDir) {
- $found = false;
-
- foreach ($rawSearchDirs as $dir) {
- $currentPrefix = realpath($dir) . DIRECTORY_SEPARATOR;
- $supposedExcluded = str_replace($currentPrefix, '', realpath($currentPrefix . $excludeDir));
- if (! $supposedExcluded) {
- continue;
- }
- if (strpos($supposedExcluded, DIRECTORY_SEPARATOR) === false) {
- $excludeDirs[] = $supposedExcluded;
- $found = true;
- break;
- }
- }
- if ($found) {
- continue;
- }
-
- $supposedExcluded = realpath($excludeDir);
- if ($supposedExcluded) {
- foreach ($rawSearchDirs as $dir) {
- $searchDir = realpath($dir) . DIRECTORY_SEPARATOR;
- $supposedRelative = str_replace($searchDir, '', $supposedExcluded);
- if (strpos($supposedRelative, DIRECTORY_SEPARATOR) !== false) {
- continue;
- }
- if (strpos($supposedExcluded, $searchDir) !== 0) {
- continue;
- }
- if ( ! trim($supposedRelative)) {
- continue;
- }
- $excludeDirs[] = $supposedRelative;
- $found = true;
- break;
- }
- }
- if (! $found) {
- throw new RuntimeException(sprintf("Invalid exclude dir %s ; Exclude dir is limited to the name of a directory at first depth", $excludeDir));
- }
- }
- return $excludeDirs;
- }
-
- protected function buildQuery()
- {
- if (! $this->dirs && ! $this->files) {
- throw new LogicException('You have not set any files or directory');
- }
- $command = '-n -q -b -X -charset UTF8';
- if ($this->recursive) {
- $command .= ' -r';
- }
- if (!empty($this->extensions)) {
- if (! $this->extensionsToggle) {
- $extensionPrefix = ' --ext';
- } else {
- $extensionPrefix = ' -ext';
- }
- foreach ($this->extensions as $extension) {
- $command .= $extensionPrefix . ' ' . escapeshellarg($extension);
- }
- }
- if (! $this->followSymLinks) {
- $command .= ' -i SYMLINKS';
- }
- if ($this->ignoreDotFile) {
- $command .= " -if '\$filename !~ /^\./'";
- }
- foreach ($this->sort as $sort) {
- $command .= ' -fileOrder ' . $sort;
- }
- foreach ($this->computeExcludeDirs($this->excludeDirs, $this->dirs) as $excludedDir) {
- $command .= ' -i ' . escapeshellarg($excludedDir);
- }
- foreach ($this->dirs as $dir) {
- $command .= ' ' . escapeshellarg(realpath($dir));
- }
- foreach ($this->files as $file) {
- $command .= ' ' . escapeshellarg(realpath($file));
- }
- return $command;
- }
- }
|