public function processCommand() { $this->watch = $this->getRoute()->getMatchedParam('watch', false); $this->fileSystem = new Filesystem(); $this->setUpServer(); $command = sprintf('php -S %s:%d -t %s %s', 'localhost', '8000', $this->getPath() . '/' . $this->getPHPoole()->getOption('output.dir'), sprintf('%s/.phpoole/router.php', $this->getPath())); $this->wlAnnonce(sprintf('Starting server (http://%s:%d)...', 'localhost', '8000')); $process = new Process($command); if (!$process->isStarted()) { // write changes cache if ($this->watch) { $finder = new Finder(); $finder->files()->name('*.md')->name('*.html')->in([$this->getPath() . '/' . $this->getPHPoole()->getOption('content.dir'), $this->getPath() . '/' . $this->getPHPoole()->getOption('layouts.dir')]); if (is_dir($this->getPath() . '/' . $this->getPHPoole()->getOption('themes.dir'))) { $finder->in($this->getPath() . '/' . $this->getPHPoole()->getOption('themes.dir')); } $resourceCache = new ResourceCacheMemory(); $resourceWatcher = new ResourceWatcher($resourceCache); $resourceWatcher->setFinder($finder); $this->fileSystem->dumpFile($this->getPath() . '/.phpoole/watch.flag', ''); } // start server try { $process->start(); Plateform::openBrowser('http://localhost:8000'); while ($process->isRunning()) { // watch changes? if ($this->watch) { $resourceWatcher->findChanges(); if ($resourceWatcher->hasChanges()) { $this->fileSystem->dumpFile($this->getPath() . '/.phpoole/changes.flag', ''); // re-generate $this->wlAlert('Changes detected!'); $callable = new Build(); call_user_func($callable, $this->getRoute(), $this->getConsole()); } } usleep(1000000); // 1 s } } catch (ProcessFailedException $e) { $this->tearDownServer(); echo $e->getMessage(); exit(2); } } }
/** * Builds a ResourceWatcher instance. * * @param string $sourceDir Source path. * @param string $destinationDir Destination path. * * @return \Yosymfony\ResourceWatcher\ResourceWatcher */ protected function buildResourceWatcher($sourceDir, $destinationDir) { $fs = new Filesystem(); $relativeDestination = rtrim($fs->makePathRelative($destinationDir, $sourceDir), '/'); $finder = new Finder(); $finder->files()->name('*.*')->in($sourceDir); if (false === strpos($relativeDestination, '..')) { $finder->exclude($relativeDestination); } $rc = new ResourceCacheMemory(); $rw = new ResourceWatcher($rc); $rw->setFinder($finder); return $rw; }
public function testRebuild() { $finder = new Finder(); $finder->files()->name('*.txt')->in($this->tmpDir); $rc = new ResourceCacheMemory(); $rw = new ResourceWatcher($rc); $rw->setFinder($finder); $this->fs->dumpFile($this->tmpDir . '/file1.txt', 'test'); $this->fs->dumpFile($this->tmpDir . '/file2.txt', 'test'); $this->fs->dumpFile($this->tmpDir . '/file3.txt', 'test'); $rw->rebuild(); $rw->findChanges(); $this->assertCount(0, $rw->getNewResources()); $this->assertCount(0, $rw->getUpdatedResources()); $this->assertCount(0, $rw->getDeletedResources()); }