appbox = $app['phraseanet.appbox']; if (count(User_Adapter::get_wrong_email_users($app)) > 0) { throw new Exception_Setup_FixBadEmailAddresses('Please fix the database before starting'); } $this->write_lock(); return $this; } /** * * @return Void */ public function __destruct() { self::remove_lock_file(); return; } /** * Add steps to do to the counter * * @param int $how_many * @return Setup_Upgrade */ public function add_steps($how_many) { $this->total_steps += (int) $how_many; $this->write_lock(); return $this; } /** * Add completed steps to the counter * * @param int $how_many * @return Setup_Upgrade */ public function add_steps_complete($how_many) { $this->completed_steps += (int) $how_many; $this->write_lock(); return $this; } /** * Set the current message * * @param string $message * @return Setup_Upgrade */ public function set_current_message($message) { $this->message = $message; $this->write_lock(); return $this; } /** * * @param type $recommendation * @param type $command */ public function addRecommendation($recommendation, $command = null) { $this->recommendations[] = array($recommendation, $command); } /** * Return an array of recommendations * * @return array */ public function getRecommendations() { return $this->recommendations; } /** * * @return float */ protected function get_percentage() { if ($this->total_steps === 0) { return 1; } return round(max(min(($this->completed_steps / $this->total_steps), 1), 0), 2); } /** * * * @return Setup_Upgrade */ protected function write_lock() { $date_obj = new DateTime(); $dumper = new Symfony\Component\Yaml\Dumper(); $datas = $dumper->dump( array( 'percentage' => $this->get_percentage() , 'total_steps' => $this->total_steps , 'completed_steps' => $this->completed_steps , 'message' => $this->message , 'last_update' => $date_obj->format(DATE_ATOM) ), 1 ); if (!file_put_contents(self::get_lock_file(), $datas)) throw new Exception_Setup_CannotWriteLockFile( sprintf('Cannot write lock file to %s', self::get_lock_file()) ); return $this; } /** * Returns true if the file exists * * @return boolean */ protected static function lock_exists() { clearstatcache(); return file_exists(self::get_lock_file()); } /** * Return the path fil to the lock file * * @return string */ public static function get_lock_file() { return __DIR__ . '/../../../tmp/upgrade.lock'; } /** * * @return Void */ protected static function remove_lock_file() { if (self::lock_exists()) { unlink(self::get_lock_file()); } return; } /** * * Returns an array containing datas about the Upgrade Status. * Contains the following keys : * - active : (booolean) tells if there's a current upgrade * - percentage : (float) a number between 0 and 1 of the current progress * - total_steps : (int) total steps * - completed_steps : (int) current complete steps * - message : (string) a message * - last_update : (string) last update in ATOM format * * * @return Array */ public static function get_status() { $active = self::lock_exists(); $datas = array( 'active' => $active , 'percentage' => 1 , 'total_steps' => 0 , 'completed_steps' => 0 , 'message' => null , 'last_update' => null ); if ($active) { $parser = new Symfony\Component\Yaml\Parser(); $datas = array_merge( $datas , $parser->parse(file_get_contents(self::get_lock_file())) ); } return $datas; } }