/** * Create empty patch file * * @throws DbPatch_Exception * @return void */ public function execute() { $type = $this->console->getOptionValue('type', null); $patchNumber = $this->console->getOptionValue('number', null); if (is_null($type) || !in_array(strtolower($type), array('php', 'sql'))) { throw new DbPatch_Exception('Invalid patch type!'); } $patch = DbPatch_Command_Patch::factory($type); $patch->setWriter($this->getWriter()); if (is_null($patchNumber)) { $branch = $this->getBranch(); $patches = $this->getPatches($branch, '*'); if (count($patches)) { $lastPatch = end($patches); $patchNumber = $lastPatch->patchNumber + 1; } else { $patchNumber = 1; } } $description = $this->console->getOptionValue('description', 'Empty Patch'); $patch->patchNumber = $patchNumber; $patch->branch = $this->getBranch(); $patch->create($description, $this->getPatchDirectory(), $this->getPatchPrefix()); }
/** * @param string $branch * @param null|int $searchPatchNumber * @return array */ public function getPatches($branch, $searchPatchNumber = null) { $patchDirectory = $this->getPatchDirectory(); if (!is_dir($patchDirectory)) { $this->writer->error('path ' . $patchDirectory . ' doesn\'t exists'); return array(); } try { $iterator = new DirectoryIterator($patchDirectory); } catch (Exception $e) { $this->writer->line('Error: ' . $e->getMessage()); return array(); } $branch = $branch == '' ? $this->getBranch() : $branch; $patchPrefix = $this->getPatchPrefix(); if ($branch != self::DEFAULT_BRANCH) { $patchPrefix .= '-' . $branch; } $patches = array(); $pattern = '/^' . preg_quote($patchPrefix) . '-(\\d{3,4})\\.(sql|php)$/'; foreach ($iterator as $fileinfo) { if ($fileinfo->isDot() || substr($fileinfo->getFilename(), 0, 1) == '.') { continue; } if (preg_match($pattern, $fileinfo->getFilename(), $matches)) { $patchNumber = (int) $matches[1]; if (!is_null($searchPatchNumber) && $searchPatchNumber != '*' && $patchNumber != $searchPatchNumber || is_null($searchPatchNumber) && $this->isPatchApplied($patchNumber, $branch)) { continue; } $filename = $patchDirectory . '/' . $fileinfo->getFilename(); $type = $matches[2]; $patch = DbPatch_Command_Patch::factory($type); $patch->loadFromArray(array('filename' => $filename, 'basename' => $matches[0], 'patchNumber' => $patchNumber, 'branch' => $branch)); $patches[$patchNumber] = $patch; } } ksort($patches); return $patches; }