/** * Generates an encryption key and inserts it into the * application/config/config.php file. */ public function generateEncryptionKey() { $length = 16; $this->ci->load->library('Encryption'); $key = bin2hex($this->ci->encryption->create_key($length)); $replace = "hex2bin( '{$key}' )"; $kit = new FileKit(); $kit->replaceIn(BUILDBASE . '../application/config/config.php', "'PLEASE_CHANGE_ME!'", $replace); }
public function regexContent(UnitTester $I) { $kit = new FileKit(); $str = "X"; $test = <<<EOF Xut stuff before here. Xut stuff after here. EOF; $kit->replaceWithRegex($this->filename, "/P/", $str); $I->openFile($this->filename); $I->canSeeFileContentsEqual($test); }
/** * Injects a block of code into an existing file. Using options * you can specify where the code should be inserted. Available options * are: * prepend - Place at beginning of file * append - Place at end of file * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") * after => '' - Insert just after this line of text (don't forget the line ending "\n") * replace => '' - a simple string to be replaced. All locations will be replaced. * regex => '' - a pregex pattern to use to replace all locations. * * @param $path * @param $content * @param array $options * * @return $this */ public function injectIntoFile($path, $content, $options = 'append') { $kit = new FileKit(); if (is_string($options)) { $action = $options; } else { if (is_array($options) && count($options)) { $keys = array_keys($options); $action = array_shift($keys); $param = $options[$action]; } } switch (strtolower($action)) { case 'prepend': $success = $kit->prepend($path, $content); break; case 'before': $success = $kit->before($path, $param, $content); break; case 'after': $success = $kit->after($path, $param, $content); break; case 'replace': $success = $kit->replaceIn($path, $param, $content); break; case 'regex': $success = $kit->replaceWithRegex($path, $param, $content); break; case 'append': default: $success = $kit->append($path, $content); break; } if ($success) { CLI::write(CLI::color("\t" . strtolower(lang('modified')) . " ", 'cyan') . str_replace(APPPATH, '', $path)); } else { CLI::write(CLI::color("\t" . strtolower(lang('error')) . " ", 'light_red') . str_replace(APPPATH, '', $path)); } return $this; }