作者:sarahsampa
项目:compare-ap
/**
* Registers the module-only services
*
* @param \Phalcon\DiInterface $di
*/
public function registerServices($di)
{
/**
* Read application wide and module only configurations
*/
$appConfig = $di->get('config');
$moduleConfig = (include __DIR__ . '/config/config.php');
$di->set('moduleConfig', $moduleConfig);
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($appConfig) {
$url = new UrlResolver();
$url->setBaseUri($appConfig->application->baseUri);
return $url;
});
/**
* Module specific dispatcher
*/
$di->set('dispatcher', function () use($di) {
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($di->getShared('eventsManager'));
$dispatcher->setDefaultNamespace('App\\Modules\\Oauth\\');
return $dispatcher;
});
/**
* Module specific database connection
*/
$di->set('db', function () use($appConfig) {
return new DbAdapter(['host' => $moduleConfig->database->host, 'username' => $moduleConfig->database->username, 'password' => $moduleConfig->database->password, 'dbname' => $moduleConfig->database->name]);
});
}
作者:lnmtie
项目:phalcon-bas
public function registerServices(DiInterface $di)
{
global $config;
$di->setShared('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->backend->baseUri);
return $url;
});
$di->setShared('dispatcher', function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Multiple\\Backend\\Controllers");
return $dispatcher;
});
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->backend->viewsDir);
$view->setLayoutsDir('layouts/');
$view->setPartialsDir('partials/');
$view->registerEngines(array('.phtml' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->backend->cacheDir, 'compiledSeparator' => '_'));
return $volt;
}, '.volt' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
});
}
作者:nicklos1
项目:littlemal
function setDi()
{
$di = new FactoryDefault();
$di['router'] = function () use($di) {
$router = new Router();
$router->setDefaultModule('mobimall');
return $router;
};
$di['url'] = function () {
$url = new UrlResolver();
$url->setBaseUri('/');
return $url;
};
$di['session'] = function () {
$session = new SessionAdapter();
// $session->start();
return $session;
};
$loader = new Loader();
$loader->registerNamespaces(array('Mall\\Mdu' => __DIR__ . '/../apps/mdu'));
$sysConfig = (include __DIR__ . '/../config/sysconfig.php');
$di['sysconfig'] = function () use($sysConfig) {
return $sysConfig;
};
$loader->register();
return $di;
}
作者:kjmtru
项目:phanboo
/**
* Register the services here to make them general
* or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(DiInterface $di)
{
//Read configuration
$config = (include __DIR__ . "/config/config.php");
// The URL component is used to generate all kind of urls in the application
$di->set('url', function () use($config) {
$url = new Url();
$url->setBaseUri($config->application->baseUri);
return $url;
});
//Registering a dispatcher
$di->set('dispatcher', function () {
//Create/Get an EventManager
$eventsManager = new EventsManager();
//Attach a listener
$eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) {
//controller or action doesn't exist
if ($event->getType() == 'beforeException') {
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(['module' => 'backend', 'controller' => 'errors', 'action' => 'notFound']);
return false;
}
}
});
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers");
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
/**
* Setting up the view component
*/
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => 'volt']);
// Create an event manager
$eventsManager = new EventsManager();
// Attach a listener for type 'view'
$eventsManager->attach('view', function ($event, $view) {
if ($event->getType() == 'notFoundView') {
throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
}
});
// Bind the eventsManager to the view component
$view->setEventsManager($eventsManager);
return $view;
});
$configMenu = (include __DIR__ . "/config/config.menu.php");
$di->setShared('menuStruct', function () use($configMenu) {
// if structure received from db table instead getting from $config
// we need to store it to cache for reducing db connections
$struct = $configMenu->get('menuStruct')->toArray();
return $struct;
});
}
作者:arius8
项目:cor
/**
* {@inheritdoc}
*/
public function register(DiInterface $di)
{
$di->set(self::SERVICE_NAME, function () use($di) {
$url = new UrlResolver();
$url->setBaseUri($di->get('config')->application->baseUri);
return $url;
}, true);
}
作者:kengo
项目:phalconCar
public function attachUrlResolver($baseUri = '/')
{
$this->_di->setShared('url', function () use($baseUri) {
$url = new \Phalcon\Mvc\Url();
$url->setBaseUri($baseUri);
return $url;
});
return $this;
}
作者:adrianeava
项目:manager.i
/**
* Register services used by the backend application
*
* @param $di
*/
public function registerServices($di)
{
$config = $this->config();
/**
* register the dispatcher
*/
$di->set('dispatcher', function () {
$dispatcher = new Dispatcher();
/*$eventManager = new Manager();
$eventManager->attach('dispatch', new \Acl('backend'));
$dispatcher->setEventsManager($eventManager);*/
$dispatcher->setDefaultNamespace('app\\backend\\controllers');
return $dispatcher;
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
$di->setShared('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(['.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(['compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_']);
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use($config) {
return new MysqlAdapter($config->database->toArray());
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
}
作者:dotronglon
项目:phalcon-engin
public function onBoot()
{
// TODO: Implement onBoot() method.
$this->getDI()->setShared('url', function () {
$protocol = stripos(server('SERVER_PROTOCOL'), 'https') === true ? 'https://' : 'http://';
$hostname = server('HTTP_HOST');
$url = new Url();
$url->setStaticBaseUri(env('static_url', "{$protocol}{$hostname}/"));
$url->setBaseUri(env('base_url', '/'));
return $url;
});
}
作者:vietdh8
项目:product-v
/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
/**
* Read configuration
*/
$config = (include APP_PATH . "/apps/backend/config/config.php");
/**
* Setting up the view component
*/
$di['view'] = function () use($config) {
$view = new View();
$view->setViewsDir(__DIR__ . '/views/');
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => __DIR__ . '/cache/', 'compiledSeparator' => '_'));
$compiler = $volt->getCompiler();
// format number
$compiler->addFilter('number', function ($resolvedArgs) {
return 'Helpers::number(' . $resolvedArgs . ');';
});
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
};
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di['db'] = function () use($config) {
$config = $config->database->toArray();
$dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter'];
unset($config['adapter']);
return new $dbAdapter($config);
};
/**
* Logger service
*/
$di->set('logger', function ($filename = null, $format = null) use($config) {
$format = $format ?: $config->get('logger')->format;
$filename = trim($filename ?: $config->get('logger')->filename, '\\/');
$path = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
$formatter = new FormatterLine($format, $config->get('logger')->date);
$logger = new FileLogger($path . $filename);
$logger->setFormatter($formatter);
$logger->setLogLevel($config->get('logger')->logLevel);
return $logger;
});
$di->set('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri("/backend/");
return $url;
});
}
作者:lison
项目:cphalco
/**
* Initializes the response object and returns it
*
* @author Nikolaos Dimopoulos <nikos@phalconphp.com>
* @since 2014-10-05
*
* @return PhTResponse
*/
protected function getResponseObject()
{
PhDI::reset();
$di = new PhDI();
$di->set('url', function () {
$url = new PhUrl();
$url->setBaseUri('/');
return $url;
});
$response = new PhTResponse();
$response->setDI($di);
return $response;
}
作者:tmquang680
项目:phale
/**
* Set url service when module start
* @param Di $di
* @param Config $config
* @param string $name Module name
* @return \Phalex\Events\Listener\Application
*/
private function setUrlService(Di $di, Config $config, $name)
{
$base = $static = '/';
if (isset($config['url'])) {
$default = isset($config['url']['default']) ? $config['url']['default'] : '/';
if (isset($config['url'][$name])) {
$base = isset($config['url'][$name]['uri']) ? $config['url'][$name]['uri'] : $default;
$static = isset($config['url'][$name]['static']) ? $config['url'][$name]['static'] : $default;
}
}
$url = new UrlService();
$url->setBaseUri($base);
$url->setStaticBaseUri($static);
$di->set('url', $url, true);
return $this;
}
作者:lison
项目:incubato
/**
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->checkExtension('phalcon');
// Reset the DI container
Di::reset();
// Instantiate a new DI container
$di = new Di();
// Set the URL
$di->set('url', function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
});
$di->set('escaper', function () {
return new Escaper();
});
$this->di = $di;
}
作者:lison
项目:cphalco
public function testGet()
{
$url = new Url();
$url->setBaseUri('http://www.test.com');
$this->assertEquals('http://www.test.com', $url->get(''));
$this->assertEquals('http://www.test.com/', $url->get('/'));
$this->assertEquals('http://www.test.com/path', $url->get('/path'));
$url->setBaseUri('http://www.test.com/?_url=/');
$this->assertEquals('http://www.test.com/?_url=/path¶ms=one', $url->get('path', array('params' => 'one')));
}
作者:vikilabo
项目:digitalkrikit
/**
* Register the services here to make them general or register in the ModuleDefinition to make them module-specific
*/
public function registerServices(\Phalcon\DiInterface $di)
{
/**
* Read configuration
*/
$config = (include __DIR__ . "/config/config.php");
/**
* Setting up the view component
*/
// The URL component is used to generate all kind of urls in the application
$di->set('url', function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
});
/**
* Setting up the view component
*/
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->view->viewsDir);
$view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
$view->registerEngines(['.volt' => function () use($view, $config) {
$volt = new Volt($view);
$volt->setOptions(['compiledPath' => $config->application->view->compiledPath, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compiledExtension' => $config->application->view->compiledExtension, 'compileAlways' => true]);
return $volt;
}]);
// Create an event manager
$eventsManager = new EventsManager();
// Attach a listener for type 'view'
$eventsManager->attach('view', function ($event, $view) {
if ($event->getType() == 'notFoundView') {
throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
}
});
// Bind the eventsManager to the view component
$view->setEventsManager($eventsManager);
return $view;
});
}
作者:nineesh
项目:incubato
/**
* Sets the test up by loading the DI container and other stuff
*
* @author Nikos Dimopoulos <nikos@phalconphp.com>
* @since 2012-09-30
* @param \Phalcon\DiInterface $di
* @param \Phalcon\Config $config
* @return void
*/
protected function setUp(DiInterface $di = null, Config $config = null)
{
$this->checkExtension('phalcon');
if (!is_null($config)) {
$this->config = $config;
}
if (is_null($di)) {
// Reset the DI container
DI::reset();
// Instantiate a new DI container
$di = new FactoryDefault();
// Set the URL
$di->set('url', function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
});
$di->set('escaper', function () {
return new \Phalcon\Escaper();
});
}
$this->di = $di;
}
作者:jein
项目:Phalcon-RestAp
private function setUrl()
{
$url = new Url();
if (!$this->application->debug) {
$url->setBaseUri($this->application->production->baseUri);
$url->setStaticBaseUri($this->application->production->staticBaseUri);
} else {
$url->setBaseUri($this->application->development->baseUri);
$url->setStaticBaseUri($this->application->development->staticBaseUri);
}
return $url;
}
作者:Akhundzad
项目:blog.toda
<?php
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
$di = new FactoryDefault();
$di->set('url', function () use($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
$di->set('view', function () use($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array('.volt' => function ($view, $di) use($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_', 'compileAlways' => true));
return $volt;
}, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
return $view;
}, true);
$di->set('db', function () use($config) {
return new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname, "charset" => $config->database->charset));
});
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
作者:simple-helper
项目:php-github-api-wra
},
true
);*/
//Github API mock
$di->set('github', function () use($config) {
$api = m::mock("api");
$api->shouldReceive("show")->with("simple-helpers", "php-file-mover", 8)->andReturn(json_decode(file_get_contents(__DIR__ . "/../_data/8"), true));
$client = m::mock("Github");
$client->shouldReceive("api")->with('pull_request')->andReturn($api);
return $client;
}, true);
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use($config) {
$url = new UrlResolver();
if (!$config->application->debug) {
$url->setBaseUri($config->application->production->baseUri);
$url->setStaticBaseUri($config->application->production->staticBaseUri);
} else {
$url->setBaseUri($config->application->development->baseUri);
$url->setStaticBaseUri($config->application->development->staticBaseUri);
}
return $url;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use($config) {
$connection = new DatabaseConnection($config->database->toArray());
$debug = $config->application->debug;
作者:lison
项目:cphalco
/**
* @ticket 1960
* @author Vladimir Kolesnikov <vladimir@extrememember.com>
* @since 2014-02-02
*/
public function testIssue1960()
{
$url = new \Phalcon\Mvc\Url();
$url->setDI($this->di);
$params = 'http://www.google.com/';
$expected = 'http://www.google.com/';
$actual = $url->get($params);
$this->assertEquals($expected, $actual, 'External Site Url not correct');
}
作者:tapio
项目:test-ph
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
try {
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(array('../app/controllers/', '../app/models/'))->register();
// Setup the database service
$di->set('db', function () {
return new DbAdapter(array("host" => "pellefant-01.db.elephantsql.com", "username" => "yxlluzxv", "password" => "XlspU6IwYJrlKnn9drdaRfpRv2PjyFR5", "dbname" => "yxlluzxv"));
});
// Create a DI
$di = new FactoryDefault();
// Setup the view component
$di->set('view', function () {
$view = new View();
$view->setViewsDir('../app/views/');
return $view;
});
// Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function () {
$url = new UrlProvider();
$url->setBaseUri('');
return $url;
});
// Handle the request
$application = new Application($di);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo "Exception: ", $e->getMessage();
}