/** * 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('Phalcon extension is not installed, follow these instructions to install it: http://phalconphp.com/documentation/install'); } if ($ip !== null) { self::$ip = $ip; } if (!defined('TEMPLATE_PATH')) { define('TEMPLATE_PATH', $path . '/templates'); } chdir('..'); // Read configuration $configPaths = array('config', 'app/config', 'apps/frontend/config'); $readed = false; foreach ($configPaths as $configPath) { $cpath = $configPath . '/config.ini'; if (file_exists($cpath)) { $config = new \Phalcon\Config\Adapter\Ini($cpath); $readed = true; break; } else { $cpath = $configPath . '/config.php'; if (file_exists($cpath)) { $config = (require $cpath); $readed = true; break; } } } if ($readed === false) { throw new \Phalcon\Exception('Configuration file could not be loaded!'); } $loader = new \Phalcon\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('Your Phalcon version is not compatible with Developer Tools, download the latest at: http://phalconphp.com/download'); } 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 \Phalcon\Mvc\Url(); $url->setBaseUri($config->application->baseUri); return $url; }); $di->set('flash', function () { return new \Phalcon\Flash\Direct(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info')); }); $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 \Phalcon\Mvc\Application(); $app->setDi($di); echo $app->handle()->getContent(); } catch (\Phalcon\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()); } }
/** * 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()); } }