/** * Js file method * * writes a js file * @param string $fileName * @param string $content */ public function _jsFile($fileName = 'all.js', $content) { $filePath = $this->_getJsFilePath() . $fileName; // file helper App::uses('File', 'Utility'); $file = new File($filePath); $file->path = $filePath; if ($file->write($file->prepare($content))) { return true; } else { return false; } }
/** * testPrepare method * * @return void */ public function testPrepare() { $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere"; if (DS === '\\') { $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; } else { $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere"; } $this->assertSame($expected, File::prepare($string)); $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; $this->assertSame($expected, File::prepare($string, true)); }
/** * Creates a file at given path * * @param string $path Where to put the file. * @param string $contents Content to put in the file. * @return boolean Success * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::createFile */ public function createFile($path, $contents) { $path = str_replace(DS . DS, DS, $path); $this->out(); if (is_file($path) && empty($this->params['force']) && $this->interactive === true) { $this->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path)); $key = $this->in(__d('cake_console', 'Do you want to overwrite?'), array('y', 'n', 'q'), 'n'); if (strtolower($key) === 'q') { $this->out(__d('cake_console', '<error>Quitting</error>.'), 2); return $this->_stop(); } elseif (strtolower($key) !== 'y') { $this->out(__d('cake_console', 'Skip `%s`', $path), 2); return false; } } else { $this->out(__d('cake_console', 'Creating file %s', $path)); } $File = new File($path, true); if ($File->exists() && $File->writable()) { $data = $File->prepare($contents); $File->write($data); $this->out(__d('cake_console', '<success>Wrote</success> `%s`', $path)); return true; } $this->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2); return false; }
/** * Creates a file at given path * * @param string $path Where to put the file. * @param string $contents Content to put in the file. * @return boolean Success * @access public */ function createFile($path, $contents) { $path = str_replace(DS . DS, DS, $path); $this->out(); $this->out(sprintf(__("Creating file %s", true), $path)); if (is_file($path) && $this->interactive === true) { $prompt = sprintf(__('File `%s` exists, overwrite?', true), $path); $key = $this->in($prompt, array('y', 'n', 'q'), 'y'); if (strtolower($key) == 'q') { $this->out(__('Quitting.', true), 2); $this->_stop(); } elseif (strtolower($key) != 'y') { $this->out(sprintf(__('Skip `%s`', true), $path), 2); return false; } } if (!class_exists('File')) { require LIBS . 'file.php'; } if ($File = new File($path, true)) { $data = $File->prepare($contents); $File->write($data); $this->out(sprintf(__('Wrote `%s`', true), $path)); return true; } else { $this->err(sprintf(__('Could not write to `%s`.', true), $path), 2); return false; } }
/** * Creates a file at given path * * @param string $path Where to put the file. * @param string $contents Content to put in the file. * @return boolean Success */ public function createFile($path, $contents) { $path = str_replace(DS . DS, DS, $path); $this->out(); if (is_file($path) && $this->interactive === true) { $this->out(__('<warning>File `%s` exists</warning>', $path)); $key = $this->in(__('Do you want to overwrite?'), array('y', 'n', 'q'), 'n'); if (strtolower($key) == 'q') { $this->out(__('<error>Quitting</error>.'), 2); $this->_stop(); } elseif (strtolower($key) != 'y') { $this->out(__('Skip `%s`', $path), 2); return false; } } else { $this->out(__('Creating file %s', $path)); } if (!class_exists('File')) { require LIBS . 'file.php'; } if ($File = new File($path, true)) { $data = $File->prepare($contents); $File->write($data); $this->out(__('<success>Wrote</success> `%s`', $path)); return true; } else { $this->err(__('<error>Could not write to `%s`</error>.', $path), 2); return false; } }
/** * testPrepare method * * @access public * @return void */ function testPrepare() { $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere"; if (DS == '\\') { $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; } else { $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere"; } $this->assertIdentical(File::prepare($string), $expected); $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; $this->assertIdentical(File::prepare($string, true), $expected); }
/** * Creates a file at given path * * @param string $path Where to put the file. * @param string $contents Content to put in the file. * @return boolean Success * @access public */ function createFile($path, $contents) { $path = str_replace(DS . DS, DS, $path); $this->out("\n" . sprintf(__("Creating file %s", true), $path)); if (is_file($path) && $this->interactive === true) { $key = $this->in(__("File exists, overwrite?", true) . " {$path}", array('y', 'n', 'q'), 'n'); if (low($key) == 'q') { $this->out(__("Quitting.", true) . "\n"); exit; } elseif (low($key) != 'y') { $this->out(__("Skip", true) . " {$path}\n"); return false; } } if (!class_exists('File')) { uses('file'); } if ($File = new File($path, true)) { $data = $File->prepare($contents); $File->write($data); $this->out(__("Wrote", true) . " {$path}"); return true; } else { $this->err(__("Error! Could not write to", true) . " {$path}.\n"); return false; } }
/** * This function writes the defaults.ini file, assuming that it is because the settings.ini has been fully upgraded to the latest version. * * @return {string} A string of data used to write to the settings.ini file. */ public function writeDefaultsIniData($siteDir = null) { $directory = !empty($siteDir) ? ROOT . DS . $siteDir . DS . 'Config' . DS : CONFIGS; App::uses('File', 'Utility'); $file = new File($directory . 'defaults.ini'); $writeData = $this->prepareSettingsIniData(); if ($file->write($file->prepare($writeData))) { return true; } else { throw new Exception(__('Write defaults.ini to %s failed', $directory)); } }
/** * Writes schema file from object or options * * @param mixed $object schema object or options array * @param array $options schema object properties to override object * @return mixed false or string written to file * @access public */ public function write($object, $options = array()) { if (is_object($object)) { $object = get_object_vars($object); $this->_build($object); } if (is_array($object)) { $options = $object; unset($object); } extract(array_merge(get_object_vars($this), $options)); $out = "class {$name}Schema extends CakeSchema {\n"; $out .= "\tvar \$name = '{$name}';\n\n"; if ($path !== $this->path) { $out .= "\tvar \$path = '{$path}';\n\n"; } if ($file !== $this->file) { $out .= "\tvar \$file = '{$file}';\n\n"; } if ($connection !== 'default') { $out .= "\tvar \$connection = '{$connection}';\n\n"; } $out .= "\tfunction before(\$event = array()) {\n\t\treturn true;\n\t}\n\n\tfunction after(\$event = array()) {\n\t}\n\n"; if (empty($tables)) { $this->read(); } foreach ($tables as $table => $fields) { if (!is_numeric($table) && $table !== 'missing') { $out .= $this->generateTable($table, $fields); } } $out .= "}\n"; $File = new File($path . DS . $file, true); $header = '$Id'; $content = "<?php \n/* {$name} schema generated on: " . date('Y-m-d H:m:s') . " : " . time() . "*/\n{$out}?>"; $content = $File->prepare($content); if ($File->write($content)) { return $content; } return false; }
/** * Css file method * * writes a css file * @param string $fileName * @param string $content */ protected function _cssFile($fileName = 'all.css', $content) { $filePath = $this->_getCssFilePath() . $fileName; // file helper App::uses('File', 'Utility'); $file = new File($filePath); $file->path = $filePath; if ($file->write($file->prepare($content))) { return true; } else { throw new Exception('Css File Not Saved'); } }