public function beforeExecuteRoute()
 {
     if (!$this->dispatcher->getParam('version') || $this->dispatcher->getParam('version') == 'latest') {
         $this->dispatcher->setParam('version', Models\Versions::maximum(array('column' => 'version', 'conditions' => 'version NOT LIKE("tmp-%")')));
     }
     if (!$this->dispatcher->getParam('language')) {
         $lang = strtolower(substr($this->request->getBestLanguage(), 0, 2));
         $lang = $lang && $this->_hasLanguage($lang) ? $lang : $this->config->defaultLang;
         $this->dispatcher->setParam('language', $lang);
     } elseif ($this->dispatcher->getParam('language') != $this->config->defaultLang && !$this->_hasLanguage()) {
         $this->forward404();
         return false;
     }
     $this->version = Models\Versions::findFirst(array('conditions' => 'version = ?0', 'bind' => array($this->dispatcher->getParam('version'))));
     if (!$this->version) {
         $this->forward404();
         return false;
     }
     if ($this->config->cache->enabled && $this->_applyCache() === false) {
         return false;
     }
     return true;
 }
 /**
  * @return void
  */
 public function generateAction()
 {
     $this->output('Checking your Phalcon version');
     if (!($sourceVersion = $this->_getSource('php_phalcon.h'))) {
         die('Sources in the specified path is not found');
     }
     $sourceVersion = ApiGenerator::getSourceVersion($sourceVersion);
     if ($this->_currentVersion != $sourceVersion) {
         die("Your current Phalcon version is '{$this->_currentVersion}', but in the sources is '{$sourceVersion}'. Task stopped");
     }
     $this->output("Ok. Your current Phalcon version is '{$this->_currentVersion}'");
     $exists = (bool) $this->db->tableExists((new Models\Versions())->getSource(), 'api');
     if (!$exists) {
         $this->output('DB schema doesn\'t exist. Creating.');
         $this->_createSchema();
     }
     $tmpVers = 'tmp-' . substr(md5(microtime()), 0, 6);
     $version = new Models\Versions();
     $version->version = $tmpVers;
     $version->changelog = $this->_getSource('../CHANGELOG');
     if (!$version->save()) {
         die(join("\n", $version->getMessages()));
     }
     $this->output('Generating API from sources');
     try {
         $list = (new \ReflectionExtension('phalcon'))->getClassNames();
         array_walk($list, [$this, '_generateClass'], $tmpVers);
     } catch (\Exception $e) {
         if ($this->db->isUnderTransaction()) {
             $this->db->rollback();
         }
         $version->delete();
         echo $e->getMessage(), PHP_EOL;
         echo $e->getFile(), ':', $e->getLine(), PHP_EOL;
         echo $e->getTraceAsString();
         die;
     }
     if (($oldVersion = Models\Versions::findFirst(['conditions' => 'version = ?0', 'bind' => [$sourceVersion]])) && !$oldVersion->delete()) {
         $version->delete();
         die($oldVersion->getMessages());
     }
     if (!$this->db->update($version->getSource(), ['version'], [$sourceVersion], "version='{$tmpVers}'")) {
         $version->delete();
         die('Unsuccessful attempt');
     }
     die('Done');
 }