/** * @param string $path * @return Evolution * @throws NoDownPlaceholderException * @throws NoUpPlaceholderException */ public static function getEvolution($path) { $evolution = new Evolution(); $basename = basename($path, '.sql'); $fileContent = file_get_contents($path); $upPosition = strpos($fileContent, EvolutionFile::UP); if ($upPosition === false) { throw new NoUpPlaceholderException($basename); } $upPosition = $upPosition == -1 ? 0 : $upPosition; $downPosition = strpos($fileContent, EvolutionFile::DOWN); if ($downPosition === false) { throw new NoDownPlaceholderException($basename); } $downPosition = $downPosition == -1 ? strlen($fileContent) : $downPosition; $up = substr($fileContent, $upPosition, $downPosition); $down = substr($fileContent, $downPosition); $evolution->setId((int) $basename)->setUp($up)->setDown($down); return $evolution; }
/** * @param $from * @param $to * @return \mtoolkit\evolution\model\evolution\Evolution[] * @throws GetEvolutionsException * @throws \Exception */ public function getEvolutions($from, $to) { $connection = $this->getConnection(); $stmt = $connection->prepare("SELECT id, up, down, inserted, executed\n FROM mt_evolutions\n WHERE ( id>=? OR ? IS NULL )\n AND ( id<=? OR ? IS NULL )\n ORDER BY id ASC;"); if ($stmt === false) { $errorMessage = sprintf("%s: %s", ErrorNumber::EN_002, $connection->error); throw new \Exception($errorMessage, $connection->errno); } $stmt->bind_param('iiii', $from, $from, $to, $to); $result = $stmt->execute(); if ($result === false) { throw new GetEvolutionsException(); } $toReturn = array(); $stmt->bind_result($id, $up, $down, $inserted, $executed); while ($stmt->fetch()) { $evolution = new Evolution(); $evolution->setId($id)->setUp($up)->setDown($down); if ($executed != null) { $evolution->setExecuted(new \DateTime($executed)); } if ($inserted != null) { $evolution->setInserted(new \DateTime($inserted)); } $toReturn[] = $evolution; } $stmt->free_result(); $stmt->close(); return $toReturn; }