public function testDetectingOfDirectoryResourceEvents()
 {
     $files = m::mock('Illuminate\\Filesystem\\Filesystem');
     $files->shouldReceive('exists')->times(20)->andReturn(false, true);
     $files->shouldReceive('lastModified')->times(19)->andReturn(time());
     $resource = new DirectoryResource(new SplFileInfo(__DIR__), $files);
     $resource->setupDirectory();
     $events = $resource->detectChanges();
     $this->assertInstanceOf('JasonLewis\\ResourceWatcher\\Event', $event = array_pop($events));
     $this->assertEquals(Event::RESOURCE_CREATED, $event->getCode());
 }
 /**
  * Detect the descendant resources of the directory.
  *
  * @return array
  */
 protected function detectDirectoryDescendants()
 {
     $descendants = [];
     foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getPath())) as $file) {
         if ($file->isDir() && !in_array($file->getBasename(), array('.', '..'))) {
             $resource = new DirectoryResource($file, $this->files);
             $descendants[$resource->getKey()] = $resource;
         } elseif ($file->isFile()) {
             $resource = new FileResource($file, $this->files);
             $descendants[$resource->getKey()] = $resource;
         }
     }
     return $descendants;
 }
Esempio n. 3
0
 /**
  * Register a resource to be watched.
  *
  * @param  string  $resource
  * @return \JasonLewis\ResourceWatcher\Listener
  */
 public function watch($resource)
 {
     if (!$this->files->exists($resource)) {
         throw new RuntimeException('Resource must exist before you can watch it.');
     } elseif ($this->files->isDirectory($resource)) {
         $resource = new DirectoryResource(new SplFileInfo($resource), $this->files);
         $resource->setupDirectory();
     } else {
         $resource = new FileResource(new SplFileInfo($resource), $this->files);
     }
     // The listener gives users the ability to bind listeners on the events
     // created when watching a file or directory. We'll give the listener
     // to the tracker so the tracker can fire any bound listeners.
     $listener = new Listener();
     $this->tracker->register($resource, $listener);
     return $listener;
 }