public function testProcessingTree() { $fs = MockFileSystem::create(false)->withFile('something.foo', 'Some contents.')->withDir('_cache')->withDir('_counter'); // Order by priority by hand... $processors = array(); $processors[] = new MockProcessor('uppercase', 'foo', function ($c) { return strtoupper($c); }, IProcessor::PRIORITY_HIGH); $processors[] = new MockProcessor('prefix', 'html', function ($c) { return 'prefixed: ' . $c; }); $processors[] = new MockProcessor('wraptag', array('foo' => 'html'), function ($c) { return "<b>{$c}</b>"; }); $processors[] = new CopyFileProcessor(); $builder = new ProcessingTreeBuilder($processors); $treeRoot = $builder->build('something.foo'); $this->assertEquals('something.foo', $treeRoot->getPath()); $this->assertEquals('uppercase', $treeRoot->getProcessor()->getName()); $outputs = $treeRoot->getOutputs(); $this->assertEquals(1, count($outputs)); $this->assertEquals('something.foo', $outputs[0]->getPath()); $this->assertEquals('wraptag', $outputs[0]->getProcessor()->getName()); $outputs = $outputs[0]->getOutputs(); $this->assertEquals(1, count($outputs)); $this->assertEquals('something.html', $outputs[0]->getPath()); $this->assertEquals('prefix', $outputs[0]->getProcessor()->getName()); $outputs = $outputs[0]->getOutputs(); $this->assertEquals(1, count($outputs)); $this->assertEquals('something.html', $outputs[0]->getPath()); $this->assertEquals('copy', $outputs[0]->getProcessor()->getName()); }
protected function bakeFile($path, $rootDir, $rootDirLength) { // Start timing this. $start = microtime(true); // Figure out the root-relative path. $relative = substr($path, $rootDirLength); // Figure out if a previously baked file has overridden this one. // This can happen for example when a theme's file (baked via a // special mount point) is overridden in the user's website (baked // via the first normal root directory). $isOverridden = false; foreach ($this->bakedFiles as $bakedFile) { if ($bakedFile['relative_input'] == $relative) { $isOverridden = true; } } if ($isOverridden) { $this->bakedFiles[$path] = array('relative_input' => $relative, 'was_baked' => false, 'was_overridden' => true); $this->logger->info(PieCrustBaker::formatTimed($start, $relative) . ' [not baked, overridden]'); return; } // Get the processing tree for that file. $builder = new ProcessingTreeBuilder($this->getProcessors(), $this->logger); $treeRoot = $builder->build($relative); // Add an entry in the baked files' metadata. $bakeDir = $this->bakeDir; $treeLeaves = $treeRoot->getLeaves(); $this->bakedFiles[$path] = array('relative_input' => $relative, 'relative_outputs' => array_map(function ($n) { return $n->getPath(); }, $treeLeaves), 'outputs' => array_map(function ($n) use($bakeDir) { return $bakeDir . $n->getPath(); }, $treeLeaves), 'was_baked' => false, 'was_overridden' => false); // See if we should force bake the file. $forceBake = !$this->parameters['smart']; if (!$forceBake) { foreach ($this->parameters['force_patterns'] as $p) { if (preg_match($p, $relative)) { $forceBake = true; break; } } } if ($forceBake) { $treeRoot->setState(ProcessingTreeNode::STATE_DIRTY, true); } // Bake! $runner = new ProcessingTreeRunner($rootDir, $this->tmpDir, $this->bakeDir, $this->logger); if ($runner->bakeSubTree($treeRoot)) { $this->bakedFiles[$path]['was_baked'] = true; $this->logger->info(PieCrustBaker::formatTimed($start, $relative)); } }