/** * @param GetResponseEvent $event */ public function onRequest(GetResponseEvent $event) { if (!$event->isMasterRequest()) { return; } if ($this->tracker->isOutdated()) { $this->compiler->compile(); } $this->dumper->dump(new Filesystem()); }
/** * Rebuild the cache, check to see if it's still valid and rebuild if it's outdated. */ public function rebuild() { if ($this->tracker->isOutdated()) { $this->logger->info('[Webpack 1/2]: Compiling assets.'); $output = $this->compiler->compile(); $this->logger->debug($output); $this->logger->info('[Webpack 2/2]: Dumping assets.'); $this->dumper->dump(); } else { $this->logger->info('[Webpack]: Cache still up-to-date.'); } }
public function testTracker() { $profiler = new Profiler(); $finder = $this->getMock(TemplateFinderInterface::class); $fixture_path = realpath(__DIR__ . '/../../Fixture'); $temp_file = $fixture_path . '/Bundle/tempfile.txt'; $temp_file2 = $fixture_path . '/Bundle/tempfile2.txt'; $temp_file3 = $fixture_path . '/Bundle/tempfile3.txt'; $tracker = new Tracker($profiler, $finder, $fixture_path . '/cache', $fixture_path, 'Resources', ['FooBundle' => $fixture_path . '/Bundle/FooBundle', 'BarBundle' => $fixture_path . '/Bundle/BarBundle']); touch($temp_file); $tracker->addPath($temp_file); $tracker->addPath($fixture_path . '/Bundle'); $finder->expects($this->once())->method('findAllTemplates')->willReturn([new TemplateReference('dont_parse_me', 'php'), new TemplateReference('@FooBundle/foo.html.twig', 'twig'), new TemplateReference('@AcmeBundle/foo.html.twig', 'twig'), new TemplateReference('template.html.twig', 'twig'), new TemplateReference('i_dont_exist', 'twig')]); $this->assertEquals('/i/cant/be/resolved', $tracker->resolveResourcePath('/i/cant/be/resolved')); // Start by removing the cache file, if it exists. if (file_exists($fixture_path . '/cache/webpack.asset_tracker.cache')) { unlink($fixture_path . '/cache/webpack.asset_tracker.cache'); } $this->assertTrue($tracker->isOutdated()); // Start by rebuilding the cache $tracker->rebuild(); // Since cache is fresh, isOutdated should return false. $this->assertFalse($tracker->isOutdated()); // Modify something. touch($temp_file, time() + 1 + mt_rand(1, 100)); $this->assertTrue($tracker->isOutdated()); // Create something $tracker->rebuild(); touch($temp_file2); $this->assertTrue($tracker->isOutdated()); unlink($temp_file); unlink($temp_file2); touch($temp_file3); $this->assertTrue($tracker->isOutdated()); $tracker->rebuild(); touch($temp_file, time() + 10); $this->assertTrue($tracker->isOutdated()); unlink($temp_file3); }
/** * The the isOutdated function in case the 'compiled' version is still fresh enough. */ public function testIsNotOutdated() { $path = tempnam(sys_get_temp_dir(), 'unittest_tracker_source'); unlink($path); mkdir($path); $output_directory = tempnam(sys_get_temp_dir(), 'unittest_tracker_output'); unlink($output_directory); mkdir($output_directory); $time = time(); $file = tempnam($path, 'unittest_tracker_source_asset'); touch($file, $time - 100); $file2 = tempnam($output_directory, 'unittest_tracker_output_compiled'); touch($file2, $time); $profiler = $this->prophesize(Profiler::class); $profiler->set('tracker.reason', false)->shouldBeCalled(); $profiler->set('bundles', [])->shouldBeCalled(); $profiler->set('templates', [])->shouldBeCalled(); $finder = $this->prophesize(TemplateFinderInterface::class); $finder->findAllTemplates()->willReturn([]); $tracker = new Tracker($profiler->reveal(), $finder->reveal(), $this->root_dir, $this->asset_dir, $output_directory); $tracker->addPath($path); self::assertFalse($tracker->isOutdated()); //Cleanup $fs = new Filesystem(); $fs->remove($path); $fs->remove($output_directory); }