/** * Write array of lines to section. * * @param string[] $lines * * @throws FileException */ private function writeLines($lines) { $oldLines = $this->fileSystem->read($this->path); $newLines = array(); $sectionExists = false; $inSection = false; foreach ($oldLines as $oldLine) { if ($this->isBeginOfSection($oldLine)) { $inSection = true; } if (!$inSection) { $newLines[] = $oldLine; } if ($this->isEndOfSection($oldLine)) { $newLines = array_merge($newLines, array('# BEGIN ' . self::$sectionLabel), $lines, array('# END ' . self::$sectionLabel)); $sectionExists = true; $inSection = false; } } if ($inSection && !$sectionExists) { throw new FileException('Missing END marker in Htaccess file.'); } if (!$sectionExists) { $newLines = array_merge($oldLines, array('# BEGIN ' . self::$sectionLabel), $lines, array('# END ' . self::$sectionLabel)); } $this->fileSystem->write($this->path, $newLines); }
function let(Filesystem $fileSystem) { $fileSystem->read('path/to/.htaccess')->willReturn(array('# BEGIN Firewall', 'order allow,deny', 'ErrorDocument 403 "You are blocked!"', 'deny from 123.0.0.1', 'deny from 123.0.0.2', 'allow from all', '# END Firewall')); $this->beConstructedWith('path/to/.htaccess', $fileSystem); }