/** * 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()); } }
<?php require __DIR__ . '/../config/services.php'; $application = new Phalcon\Mvc\Application(); $application->setDi($di); require __DIR__ . '/../config/modules.php'; echo $application->handle()->getContent();
public function testApplicationModulesDefinitionClosure() { // Creates the autoloader $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array('Frontend\\Controllers' => __DIR__ . '/modules/frontend/controllers/', 'Backend\\Controllers' => __DIR__ . '/modules/backend/controllers/')); $loader->register(); $_GET['_url'] = '/login'; Phalcon\DI::reset(); $di = new Phalcon\DI\FactoryDefault(); $di->set('router', function () { $router = new Phalcon\Mvc\Router(false); $router->add('/index', array('controller' => 'index', 'module' => 'frontend', 'namespace' => 'Frontend\\Controllers\\')); $router->add('/login', array('controller' => 'login', 'module' => 'backend', 'namespace' => 'Backend\\Controllers\\')); return $router; }); $application = new Phalcon\Mvc\Application(); $view = new \Phalcon\Mvc\View(); $application->registerModules(array('frontend' => function ($di) use($view) { $di->set('view', function () use($view) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { $di->set('view', function () use($view) { $view->setViewsDir(__DIR__ . '/modules/backend/views/'); return $view; }); })); $application->setDi($di); $this->assertEquals($application->handle()->getContent(), '<html>here</html>' . PHP_EOL); $loader->unregister(); }
/** * Executes the web tool application * * @param string $path */ public static function main($path) { chdir('..'); if (!extension_loaded('phalcon')) { throw new \Exception('Phalcon extension isn\'t installed, follow these instructions to install it: http://phalconphp.com/documentation/install'); } //Read configuration $configPath = "app/config/config.ini"; if (file_exists($configPath)) { $config = new \Phalcon\Config\Adapter\Ini($configPath); } else { $configPath = "app/config/config.php"; if (file_exists($configPath)) { $config = (require $configPath); } else { 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 isn\'t compatible with Developer Tools, download the latest at: http://phalconphp.com/download'); } if (!defined('TEMPLATE_PATH')) { define('TEMPLATE_PATH', $path . '/templates'); } try { $di = new \Phalcon\Di\FactoryDefault(); $di->set('view', function () use($path) { $view = new \Phalcon\Mvc\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) { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); }); 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()); } }