Example #1
0
    public function afterContentDoubleCheck(UnitTester $I)
    {
        $kit = new FileKit();
        $str = "New Content";
        $test = <<<EOF
Put stuff before here.
New Content

Put stuff after here.

EOF;
        $kit->after($this->filename, "Put stuff before here.\n", $str);
        $I->openFile($this->filename);
        $I->canSeeFileContentsEqual($test);
    }
Example #2
0
 /**
  * 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;
 }