Ejemplo n.º 1
0
 /**
  * Detect any changes to the resource.
  *
  * @return array
  */
 public function detectChanges()
 {
     $events = parent::detectChanges();
     // When a descendant file is created or deleted a modified event is fired on the
     // directory. This is the only way a directory will receive a modified event and
     // will thus result in two events being fired for a single descendant modification
     // within the directory. This will clear the events if we got a modified event.
     if (!empty($events) && $events[0]->getCode() == Event::RESOURCE_MODIFIED) {
         $events = [];
     }
     foreach ($this->descendants as $key => $descendant) {
         $descendantEvents = $descendant->detectChanges();
         foreach ($descendantEvents as $event) {
             if ($event instanceof Event && $event->getCode() == Event::RESOURCE_DELETED) {
                 unset($this->descendants[$key]);
             }
         }
         $events = array_merge($events, $descendantEvents);
     }
     // If this directory still exists we'll check the directories descendants again for any
     // new descendants.
     if ($this->exists) {
         foreach ($this->detectDirectoryDescendants() as $key => $descendant) {
             if (!isset($this->descendants[$key])) {
                 $this->descendants[$key] = $descendant;
                 $events[] = new Event($descendant, Event::RESOURCE_CREATED);
             }
         }
     }
     return $events;
 }
Ejemplo n.º 2
0
 public function testDetectingOfResourceDeleted()
 {
     $files = m::mock('Illuminate\\Filesystem\\Filesystem');
     $files->shouldReceive('exists')->once()->andReturn(true);
     $files->shouldReceive('exists')->once()->andReturn(false);
     $files->shouldReceive('lastModified')->once()->andReturn(time());
     $resource = new FileResource(new SplFileInfo(__FILE__), $files);
     $events = $resource->detectChanges();
     $this->assertInstanceOf('JasonLewis\\ResourceWatcher\\Event', $event = array_pop($events));
     $this->assertEquals(Event::RESOURCE_DELETED, $event->getCode());
 }