Example #1
0
 /**
  * Execute Phalcon Developer Tools
  *
  * @param  string             $path The path to the Phalcon Developer Tools
  * @param  string             $ip   Optional IP address for securing Developer Tools
  * @return void
  * @throws \Exception         if Phalcon extension is not installed
  * @throws \Exception         if Phalcon version is not compatible Developer Tools
  * @throws \Phalcon\Exception if Application config could not be loaded
  */
 public static function main($path, $ip = null)
 {
     if (!extension_loaded('phalcon')) {
         throw new \Exception(sprintf("Phalcon extension isn't installed, follow these instructions to install it: %s", Script::DOC_INSTALL_URL));
     }
     if ($ip !== null) {
         self::$ip = $ip;
     }
     if (!defined('TEMPLATE_PATH')) {
         define('TEMPLATE_PATH', $path . '/templates');
     }
     $basePath = dirname(getcwd());
     // Dirs for search config file
     $configDirs = array($basePath . '/config/', $basePath . '/app/config/', $basePath . '/apps/frontend/config/', $basePath . '/apps/backend/config/');
     $readed = false;
     foreach ($configDirs as $configPath) {
         if (file_exists($configPath . 'config.ini')) {
             $config = new ConfigIni($configPath . 'config.ini');
             $readed = true;
             break;
         } else {
             if (file_exists($configPath . 'config.php')) {
                 $config = (include $configPath . 'config.php');
                 if (is_array($config)) {
                     $config = new Config($config);
                 }
                 $readed = true;
                 break;
             }
         }
     }
     if ($readed === false) {
         throw new Exception(sprintf('Configuration file could not be loaded! Scanned dirs: %s', implode(', ', $configDirs)));
     }
     $loader = new Loader();
     $loader->registerDirs(array($path . '/scripts/', $path . '/scripts/Phalcon/Web/Tools/controllers/'));
     $loader->registerNamespaces(array('Phalcon' => $path . '/scripts/'));
     $loader->register();
     if (Version::getId() < Script::COMPATIBLE_VERSION) {
         throw new \Exception(sprintf("Your Phalcon version isn't compatible with Developer Tools, download the latest at: %s", Script::DOC_DOWNLOAD_URL));
     }
     try {
         $di = new FactoryDefault();
         $di->set('view', function () use($path) {
             $view = new View();
             $view->setViewsDir($path . '/scripts/Phalcon/Web/Tools/views/');
             return $view;
         });
         $di->set('config', $config);
         $di->set('url', function () use($config) {
             $url = new Url();
             $url->setBaseUri($config->application->baseUri);
             return $url;
         });
         $di->set('flash', function () {
             return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
         });
         $di->set('db', function () use($config) {
             if (isset($config->database->adapter)) {
                 $adapter = $config->database->adapter;
             } else {
                 $adapter = 'Mysql';
             }
             if (is_object($config->database)) {
                 $configArray = $config->database->toArray();
             } else {
                 $configArray = $config->database;
             }
             $className = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
             unset($configArray['adapter']);
             return new $className($configArray);
         });
         self::$di = $di;
         $app = new Application();
         $app->setDi($di);
         echo $app->handle()->getContent();
     } catch (Exception $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     } catch (\PDOException $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     }
 }