/** * Write the files that need to be stored * * @return void */ protected function _writeFiles() { $overwriteAll = false; if (!empty($this->params['overwrite'])) { $overwriteAll = true; } foreach ($this->_storage as $category => $domains) { foreach ($domains as $domain => $sentences) { $output = $this->_writeHeader(); foreach ($sentences as $sentence => $header) { $output .= $header . $sentence; } $filename = $domain . '.pot'; if ($category === 'LC_MESSAGES') { $File = new File($this->_output . $filename); } else { new Folder($this->_output . $category, true); $File = new File($this->_output . $category . DS . $filename); } $response = ''; while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') { $this->out(); $response = $this->in(__d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename), ['y', 'n', 'a'], 'y'); if (strtoupper($response) === 'N') { $response = ''; while (!$response) { $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename); $File = new File($this->_output . $response); $filename = $response; } } elseif (strtoupper($response) === 'A') { $overwriteAll = true; } } $File->write($output); $File->close(); } } }
/** * Reads out a file, and echos the content to the client. * * @param File $file File object * @param array $range The range to read out of the file. * @return bool True is whole file is echoed successfully or false if client connection is lost in between */ protected function _sendFile($file, $range) { $compress = $this->outputCompressed(); $file->open('rb'); $end = $start = false; if ($range) { list($start, $end) = $range; } if ($start !== false) { $file->offset($start); } $bufferSize = 8192; set_time_limit(0); session_write_close(); while (!feof($file->handle)) { if (!$this->_isActive()) { $file->close(); return false; } $offset = $file->offset(); if ($end && $offset >= $end) { break; } if ($end && $offset + $bufferSize >= $end) { $bufferSize = $end - $offset + 1; } echo fread($file->handle, $bufferSize); if (!$compress) { $this->_flushBuffer(); } } $file->close(); return true; }
/** * testMimeTypeFalse method * * @expectedException \Cake\Error\Exception * @return void */ public function testMimeTypeFalse() { $image = CORE_PATH . 'Cake/Test/TestApp/webroot/img/cake.power.gif'; $File = new File($image, false); $this->skipIf($File->mime(), 'mimeType can be determined, no Exception will be thrown'); Validation::mimeType($image, array('image/gif')); }
/** * Copy the TestApp route file so it can be modified. * * @return void */ protected function _cloneRoutes() { $File = new File(TEST_APP . 'TestApp/Config/routes.php'); $contents = $File->read(); mkdir(TMP . 'BakeTestApp/Config/', 0777, true); $File = new File(TMP . 'BakeTestApp/Config/routes.php'); $File->write($contents); }
/** * testReplaceText method * * @return void */ public function testReplaceText() { $TestFile = new File(TEST_APP . 'vendor/welcome.php'); $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp'); // Copy the test file to the temporary location $TestFile->copy($TmpFile->path, true); // Replace the contents of the tempory 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 = array('This is the', 'welcome.php file', 'in tmp directory'); $replace = array('This should be a', 'welcome.tmp file', 'in the Lib directory'); // Replace the contents of the tempory 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(); }
/** * test item() with enclosure data. * * @return void */ public function testItemEnclosureLength() { if (!is_writable(WWW_ROOT)) { $this->markTestSkipped('Webroot is not writable.'); } $testExists = is_dir(WWW_ROOT . 'tests'); $tmpFile = WWW_ROOT . 'tests/cakephp.file.test.tmp'; $File = new File($tmpFile, true); $this->assertTrue($File->write('123'), 'Could not write to ' . $tmpFile); clearstatcache(); $item = array('title' => array('value' => 'My Title', 'cdata' => true), 'link' => 'http://www.example.com/1', 'description' => array('value' => 'descriptive words', 'cdata' => true), 'enclosure' => array('url' => '/tests/cakephp.file.test.tmp'), 'pubDate' => '2008-05-31 12:00:00', 'guid' => 'http://www.example.com/1', 'category' => array(array('value' => 'CakePHP', 'cdata' => true, 'domain' => 'http://www.cakephp.org'), array('value' => 'Bakery', 'cdata' => true))); $result = $this->Rss->item(null, $item); if (!function_exists('mime_content_type')) { $type = null; } else { $type = mime_content_type($tmpFile); } $expected = array('<item', '<title', '<![CDATA[My Title]]', '/title', '<link', 'http://www.example.com/1', '/link', '<description', '<![CDATA[descriptive words]]', '/description', 'enclosure' => array('url' => $this->Rss->url('/tests/cakephp.file.test.tmp', true), 'length' => filesize($tmpFile), 'type' => $type), '<pubDate', date('r', strtotime('2008-05-31 12:00:00')), '/pubDate', '<guid', 'http://www.example.com/1', '/guid', 'category' => array('domain' => 'http://www.cakephp.org'), '<![CDATA[CakePHP]]', '/category', '<category', '<![CDATA[Bakery]]', '/category', '/item'); if ($type === null) { unset($expected['enclosure']['type']); } $this->assertTags($result, $expected); $File->delete(); if (!$testExists) { $Folder = new Folder(WWW_ROOT . 'tests'); $Folder->delete(); } }
/** * testDirSize method * * @return void */ public function testDirSize() { $path = TMP . 'tests/'; $Folder = new Folder($path . 'config_non_existent', true); $this->assertEquals(0, $Folder->dirSize()); $File = new File($Folder->pwd() . DS . 'my.php', true, 0777); $File->create(); $File->write('something here'); $File->close(); $this->assertEquals(14, $Folder->dirSize()); }
/** * Read the file contents and return a base64 version of the file contents. * * @param string $path The absolute path to the file to read. * @return string File contents in base64 encoding */ protected function _readFile($path) { $File = new File($path); return chunk_split(base64_encode($File->read())); }
/** * Enables Configure::read('Routing.prefixes') in /app/Config/routes.php * * @param string $name Name to use as admin routing * @return bool Success */ public function cakeAdmin($name) { $path = $this->appPath ?: APP; $path .= 'Config/'; $File = new File($path . 'routes.php'); $contents = $File->read(); if (preg_match('%(\\s*[/]*Configure::write\\(\'Routing.prefixes\',[\\s\'a-z,\\)\\(\\]\\[]*\\);)%', $contents, $match)) { $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', [\'' . $name . '\']);', $contents); if ($File->write($result)) { Configure::write('Routing.prefixes', [$name]); return true; } } return false; }
/** * Update the app's bootstrap.php file. * * @param string $plugin Name of plugin * @return void */ protected function _modifyBootstrap($plugin) { $bootstrap = new File($this->bootstrap, false); $contents = $bootstrap->read(); if (!preg_match("@\n\\s*Plugin::loadAll@", $contents)) { $bootstrap->append("\nPlugin::load('{$plugin}', ['bootstrap' => false, 'routes' => false]);\n"); $this->out(''); $this->out(sprintf('%s modified', $this->bootstrap)); } }
/** * Creates a file at given path * * @param string $path Where to put the file. * @param string $contents Content to put in the file. * @return bool 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->_io->out(); if (is_file($path) && empty($this->params['force'])) { $this->_io->out(__d('cake_console', '<warning>File `%s` exists</warning>', $path)); $key = $this->_io->askChoice(__d('cake_console', 'Do you want to overwrite?'), ['y', 'n', 'q'], 'n'); if (strtolower($key) === 'q') { $this->_io->out(__d('cake_console', '<error>Quitting</error>.'), 2); return $this->_stop(); } elseif (strtolower($key) !== 'y') { $this->_io->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->_io->out(__d('cake_console', '<success>Wrote</success> `%s`', $path)); return true; } $this->_io->err(__d('cake_console', '<error>Could not write to `%s`</error>.', $path), 2); return false; }