function it_should_listen_to_file_system_delete_event(Listener $listener) { $newFile = MFS::$tmpDir . '/delete.txt'; touch($newFile); $listener->getPaths()->shouldBeCalled()->willReturn(array(MFS::$tmpDir)); $listener->hasPath(Argument::any())->shouldBeCalled()->willReturn(true); $this->beConstructedWith(null); $this->initialize($listener); unlink($newFile); $this->evaluate(); $this->getChangeSet()->shouldHaveCount(1); }
/** * @param $path * @throws \PhpGuard\Listen\Exception\InvalidArgumentException * @internal param \Symfony\Component\Finder\SplFileInfo $spl */ public function checkPath($path) { $id = PathUtil::createPathID($path); if (!$this->has($id)) { if (!is_readable($path)) { return; } if (!$path instanceof SplFileInfo) { $absPath = realpath($path); foreach ($this->listener->getPaths() as $baseDir) { $baseDir = realpath($baseDir); $baseDirLen = strlen($baseDir); if ($baseDir === substr($absPath, 0, $baseDirLen)) { $path = PathUtil::createSplFileInfo($baseDir, $absPath); break; } } } if (!$path instanceof SplFileInfo) { throw new InvalidArgumentException(sprintf('Path "%s" can not registered', $path)); } if (!$this->listener->hasPath($path)) { return; } // path is new if ($path->isFile()) { $resource = new FileResource($path); } else { $resource = new DirectoryResource($path); } $tracked = $this->createTrackedObject($resource); $this->add($tracked); $this->addChangeSet($tracked, FilesystemEvent::CREATE); return; } $tracked = $this->get($id); $origin = $tracked->getResource(); if ($tracked->getChecksum() !== $origin->getChecksum() && $origin->isExists()) { // path is modified $this->addChangeSet($tracked, FilesystemEvent::MODIFY); $tracked->setChecksum($origin->getChecksum()); unset($this->unchecked[$id]); return; } if (!$origin->isExists()) { // path is deleted $this->addChangeSet($tracked, FilesystemEvent::DELETE); $this->remove($tracked); } }
function let(AdapterInterface $adapter, Listener $listener, TrackedObject $tracked, ResourceInterface $resource) { MFS::mkdir(MFS::$tmpDir); touch($this->testFile = MFS::$tmpDir . '/test.txt'); $listener->getPaths()->willReturn(array(MFS::$tmpDir)); $listener->getIgnores()->willReturn(array()); $listener->getPatterns()->willReturn(array()); $listener->hasPath(Argument::any())->willReturn(true); $tracked->getID()->willReturn(PathUtil::createPathID($this->testFile)); $tracked->getResource()->willReturn($resource); $tracked->getResource()->willReturn($resource); $resource->getResource()->willReturn(PathUtil::createSplFileInfo(MFS::$tmpDir, $this->testFile)); $resource->__toString()->willReturn($this->testFile); $this->beConstructedWith($adapter); $this->fileOnly(false); $this->add($tracked); $this->initialize($listener); }
/** * Initialize a listener * * @param Listener $listener * @return void */ public function initialize(Listener $listener) { $this->listener = $listener; $this->topDirs = array_merge($this->topDirs, $listener->getPaths()); $this->tracker->initialize($listener); }