/** * Rotate log file if size specified in config is reached. * Also if `rotate` count is reached oldest file is removed. * * @return mixed True if rotated successfully or false in case of error. * Void if file doesn't need to be rotated. */ protected function _rotateLines() { $filepath = $this->_path . $this->_file; $file = new File($filepath); $fileContent = $file->read(); $fileLines = explode(PHP_EOL, $fileContent); if (!$file->exists() and count($fileLines) < $this->_maxLine) { return; } while (count($fileLines) >= $this->_maxLine) { array_shift($fileLines); } return $file->write(implode(PHP_EOL, $fileLines)); }
/** * Update the applications plugins.php file. * * @param string $plugin Name of plugin. * @return bool If modify passed. */ protected function _modifyBootstrap($plugin) { $finder = "/\nPluginLoader::load\\((.|.\n|\n\\s\\s|\n\t|)+'{$plugin}'(.|.\n|)+\\);\n/"; $plugins = new File($this->_pluginsFile, false); $contents = $plugins->read(); if (!preg_match("@\n\\s*nPluginLoader::loadAll@", $contents)) { $contents = preg_replace($finder, "", $contents); $plugins->write($contents); $this->out(''); $this->out(sprintf('%s modified', $this->_pluginsFile)); return true; } return false; }
/** * Update the applications plugins.php file. * * @param string $plugin Name of plugin. * @param bool $hasBootstrap Whether or not bootstrap should be loaded. * @param bool $hasRoutes Whether or not routes should be loaded. * @param bool $hasAutoloader Whether or not there is an autoloader configured for * the plugin. * @return bool If modify passed. */ protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader) { $bootstrap = new File($this->_pluginsFile, false); $contents = $bootstrap->read(); if (!preg_match("@\n\\s*PluginLoader::loadAll@", $contents)) { $autoloadString = $hasAutoloader ? "'autoload' => true" : ''; $bootstrapString = $hasBootstrap ? "'bootstrap' => true" : ''; $routesString = $hasRoutes ? "'routes' => true" : ''; $append = "\\PluginLoader::load('%s', [%s]);\n"; $options = implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString])); $bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options))); $this->out(''); $this->out(sprintf('%s modified', $this->_pluginsFile)); return true; } return false; }
/** * testReplaceText method * * @return void */ public function testReplaceText() { $TestFile = new File(TEST_ROOT . '/Fixture/FileTest.txt'); $TmpFile = new File(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'cakephp.file.test.tmp', true); // Copy the test file to the temporary location $TestFile->copy($TmpFile->getPath(), true); // Replace the contents of the temporary file $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp'); $this->assertTrue($result); // Double check $expected = 'This is the welcome.tmp file in vendors directory'; $contents = $TmpFile->read(); $this->assertContains($expected, $contents); $search = ['This is the', 'welcome.php file', 'in tmp directory']; $replace = ['This should be a', 'welcome.tmp file', 'in the Lib directory']; // Replace the contents of the temporary file $result = $TmpFile->replaceText($search, $replace); $this->assertTrue($result); // Double check $expected = 'This should be a welcome.tmp file in vendors directory'; $contents = $TmpFile->read(); $this->assertContains($expected, $contents); $TmpFile->delete(); }