/** * Extract valid theme archive. * * @return $this|void */ public function extract() { $Zip = $this->zip(); $Theme = ThemeExtension::getInstance(); $name = $this->_attr->get('name'); $path = $Theme->getPath() . $name . DS; $alias = Plugin::nameToAlias($name); $isExits = $this->table()->findByAlias($alias)->first(); if (is_dir($path) && $isExits !== null) { $this->_setOutput(__d('extensions', 'The theme «{0}» already exits.', sprintf('<strong>%s</strong>', $name))); } new Folder($path); $tmp = $this->_request->data($this->_inputName); $Zip->open($tmp); $Zip->extractTo($path); if (!empty($this->_rootPath[$tmp])) { $old = $path . DS . $this->_rootPath[$tmp]; $new = $path; $Folder = new Folder($old); $Folder->move($new); } $this->_setResult(true); $this->_setOutput(__d('extensions', 'The theme «{0}» has bin successful uploaded.', sprintf('<strong>%s</strong>', $name)), true); return $this; }
/** * Installing plugin * @param null $zipPath * @return array|bool * @throws CakeException */ public function install($zipPath = null) { if (!file_exists($zipPath)) { throw new Exception(__d('spider', 'Invalid plugin file path')); } $pluginInfo = $this->getPluginMeta($zipPath); $pluginHomeDir = App::path('Plugin'); $pluginHomeDir = reset($pluginHomeDir); $pluginPath = $pluginHomeDir . $pluginInfo->name . DS; if (is_dir($pluginPath)) { throw new Exception(__d('spider', 'Plugin already exists')); } $Zip = new \ZipArchive(); if ($Zip->open($zipPath) === true) { new Folder($pluginPath, true); $Zip->extractTo($pluginPath); if (!empty($pluginInfo->rootPath)) { $old = $pluginPath . $pluginInfo->rootPath; $new = $pluginPath; $Folder = new Folder($old); $Folder->move($new); } $Zip->close(); return (array) $pluginInfo; } else { throw new CakeException(__d('spider', 'Failed to extract plugin')); } return false; }
/** * Extracts the current ZIP package. * * @param string $file Full path to the ZIP package * @return bool True on success */ protected function _unzip($file) { include_once Plugin::classPath('Installer') . 'Lib/pclzip.lib.php'; $File = new File($file); $to = normalizePath($File->folder()->pwd() . '/' . $File->name() . '_unzip/'); if (is_readable($to)) { $folder = new Folder($to); $folder->delete(); } else { $folder = new Folder($to, true); } $PclZip = new \PclZip($file); $PclZip->delete(PCLZIP_OPT_BY_EREG, '/__MACOSX/'); $PclZip->delete(PCLZIP_OPT_BY_EREG, '/\\.DS_Store$/'); if ($PclZip->extract(PCLZIP_OPT_PATH, $to)) { list($directories, $files) = $folder->read(false, false, true); if (count($directories) === 1 && empty($files)) { $container = new Folder($directories[0]); $container->move(['to' => $to]); } $this->_workingDir = $to; return true; } $this->err(__d('installer', 'Unzip error: {0}', [$PclZip->errorInfo(true)])); return false; }
public function testMoveWithoutRecursive() { extract($this->_setupFilesystem()); $Folder = new Folder($folderOne); $result = $Folder->move(['to' => $folderTwo, 'recursive' => false]); $this->assertTrue($result); $this->assertTrue(file_exists($folderTwo . '/file1.php')); $this->assertFalse(is_dir($folderTwo . '/folderA')); $this->assertFalse(file_exists($folderTwo . '/folderA/fileA.php')); }
/** * testMoveWithSkip method * * Verify that directories and files are moved recursively * even if the destination directory already exists. * Subdirectories existing in both destination and source directory * are skipped and not merged or overwritten. * * @return void */ public function testMoveWithSkip() { extract($this->_setupFilesystem()); $Folder = new Folder($folderOne); $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]); $this->assertTrue($result); $this->assertTrue(file_exists($folderTwo . '/file1.php')); $this->assertTrue(is_dir($folderTwo . '/folderB')); $this->assertTrue(file_exists($folderTwoB . '/fileB.php')); $this->assertFalse(file_exists($fileOne)); $this->assertFalse(file_exists($folderOneA)); $this->assertFalse(file_exists($fileOneA)); $Folder = new Folder($folderTwo); $Folder->delete(); new Folder($folderOne, true); new Folder($folderOneA, true); new Folder($folderTwo, true); touch($fileOne); touch($fileOneA); $Folder = new Folder($folderOne); $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]); $this->assertTrue($result); $this->assertTrue(file_exists($folderTwo . '/file1.php')); $this->assertTrue(is_dir($folderTwo . '/folderA')); $this->assertTrue(file_exists($folderTwo . '/folderA/fileA.php')); $this->assertFalse(file_exists($fileOne)); $this->assertFalse(file_exists($folderOneA)); $this->assertFalse(file_exists($fileOneA)); $Folder = new Folder($folderTwo); $Folder->delete(); new Folder($folderOne, true); new Folder($folderOneA, true); new Folder($folderTwo, true); new Folder($folderTwoB, true); touch($fileOne); touch($fileOneA); file_put_contents($folderTwoB . '/fileB.php', 'untouched'); $Folder = new Folder($folderOne); $result = $Folder->move(['to' => $folderTwo, 'scheme' => Folder::SKIP]); $this->assertTrue($result); $this->assertTrue(file_exists($folderTwo . '/file1.php')); $this->assertEquals('untouched', file_get_contents($folderTwoB . '/fileB.php')); $this->assertFalse(file_exists($fileOne)); $this->assertFalse(file_exists($folderOneA)); $this->assertFalse(file_exists($fileOneA)); $Folder = new Folder($path); $Folder->delete(); }
/** * Write staged changes * * If it's a dry run though - only show what will be done, don't do anything * * @param string $path file path * @return void */ public function commit($path = null) { if (!$path) { foreach (array_keys($this->_staged['change']) as $path) { $this->commit($path); } foreach ($this->_staged['move'] as $path => $to) { if (isset($this->_staged['change'][$path])) { continue; } $this->commit($path); } foreach ($this->_staged['delete'] as $path) { $this->commit($path); } $Folder = new Folder(TMP . 'upgrade'); $Folder->delete(); return; } $dryRun = !empty($this->params['dry-run']); $isMove = isset($this->_staged['move'][$path]); $isChanged = isset($this->_staged['change'][$path]) && count($this->_staged['change'][$path]) > 1; $isDelete = in_array($path, $this->_staged['delete']); if (!$isMove && !$isChanged && !$isDelete) { return; } $gitCd = sprintf('cd %s && ', escapeshellarg(dirname($path))); if ($isDelete) { $this->out(sprintf('<info>Delete %s</info>', Debugger::trimPath($path))); if ($dryRun) { return true; } if (!empty($this->params['git'])) { exec($gitCd . sprintf('git rm -f %s', escapeshellarg($path))); return; } if (is_dir($path)) { $Folder = new Folder($path); return $Folder->delete(); } $File = new File($path, true); return $File->delete(); } if ($isMove && !$isChanged) { $to = $this->_staged['move'][$path]; $this->out(sprintf('<info>Move %s to %s</info>', Debugger::trimPath($path), Debugger::trimPath($to))); if ($dryRun || !file_exists($path)) { return true; } if (!empty($this->params['git'])) { return $this->_gitMove($gitCd, $path, $to); } if (is_dir($path)) { $Folder = new Folder($path); return $Folder->move($to); } $File = new File($to, true); return $File->write(file_get_contents($path)) && unlink($path); } $start = reset($this->_staged['change'][$path]); end($this->_staged['change'][$path]); $final = end($this->_staged['change'][$path]); $oPath = TMP . 'upgrade' . DS . $start; $uPath = TMP . 'upgrade' . DS . $final; exec('git diff --no-index ' . escapeshellarg($oPath) . ' ' . escapeshellarg($uPath), $output); $output = implode($output, "\n"); $i = strrpos($output, $final); $diff = substr($output, $i + 41); if ($isMove) { $to = $this->_staged['move'][$path]; $this->out(sprintf('<info>Move %s to %s and update</info>', Debugger::trimPath($path), Debugger::trimPath($to))); } else { $this->out(sprintf('<info>Update %s</info>', Debugger::trimPath($path))); } $this->out($diff, 1, $dryRun ? Shell::NORMAL : SHELL::VERBOSE); if ($dryRun || !file_exists($path)) { return true; } if ($isMove) { if (!empty($this->params['git'])) { $this->_gitMove($gitCd, $path, $to); } else { unlink($path); } $path = $to; } $File = new File($path, true); return $File->write(file_get_contents($uPath)); }