<?php

use Phalcon\Storage\SerializerFactory;
use Phalcon\Mvc\Model\Transaction\Manager as TxManager;

Phalcon\Mvc\Model::setup([
  'notNullValidations'    => false,
  'exceptionOnFailedSave' => true,
  'ignoreUnknownColumns'  => true
]);

//The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
$di = new Phalcon\DI\FactoryDefault();
//inject config
$di->set('config', $config, true);
//设置cookies和加密模式
$di->setShared('cookies', function () {
	$cookies = new Phalcon\Http\Response\Cookies();
	$cookies->useEncryption(false); //如果为true则使用下面的crypt加密 , 如果使用加密那速度非常慢, 默认为加密
	return $cookies;
});
$di->setShared('url', function () use ($config) {
	$url = new Phalcon\Url();
	$url->setBaseUri($config->application->baseUri);
	return $url;
});
// Setting up the view component
$di->setShared('view', function () use ($config) {
	$view = new Phalcon\Mvc\View();
	$view->disable(); //模板渲染级别
	return $view;
});
// debug模式记录查询SQL日志
$di->setShared('db', function () use ($config) {
	$db = new Phalcon\Db\Adapter\PdoFactory();
	$db = $db->newInstance($config->database->adapter, [
		'host'       => $config->database->host,
		'username'   => $config->database->username,
		'password'   => $config->database->password,
		'port'       => $config->database->port,
		'dbname'     => $config->database->dbname,
		'charset'    => $config->database->charset,
		'persistent' => $config->database->persistent,
		'options'    => [PDO::ATTR_TIMEOUT => 5,]
	]);
	if (DEBUG) {
		$adapter = new Phalcon\Logger\Adapter\Stream(DIR_LOG . 'db_' . date('y_m_d') . '.log');
		$logger  = new Phalcon\Logger('messages', ['main' => $adapter,]);
		$manager = new Phalcon\Events\Manager();
		$manager->attach('db', function (Phalcon\Events\Event $event, $db) use ($logger) {
			($event->getType() == 'beforeQuery') && $logger->log(7, $db->getRealSQLStatement() . ' ' . json_encode($db->getSQLVariables(), 320));
		});
		$db->setEventsManager($manager);
	}
	return $db;
});
// debug模式不缓存
$di->set('modelsMetadata', function () use ($config) {
	return new Phalcon\Mvc\Model\MetaData\Stream(['metaDataDir' => DIR_METADATA,]);
});

// session
$di->setShared('session', function () use ($config) {
	$session = new Phalcon\Session\Manager();
	$adapter = new Phalcon\Session\Adapter\Redis(new Phalcon\Storage\AdapterFactory(new SerializerFactory()), [
		'host'       => $config->redis->host,
		'port'       => $config->redis->port,
		'persistent' => $config->redis->persistent,
		'lifetime'   => 86400, # 7天
		'prefix'     => $config->redis->sess_prefix,
	]);
	session_set_cookie_params(86400, '/', null, false, true);
	$session->setAdapter($adapter)->start();
	return $session;
});
// cache
$cache = new Phalcon\Cache((new Phalcon\Cache\AdapterFactory(new SerializerFactory()))->newInstance('redis', [
	'prefix'     => $config->redis->prefix,
	'lifetime'   => $config->redis->lifetime,
	'host'       => $config->redis->host,
	'port'       => $config->redis->port,
	'persistent' => $config->redis->persistent,
]));
$di->setShared('cache', function () use ($cache) {
	return $cache;
});
$di->setShared('modelsCache', function () use ($cache) {
	return $cache;
});
// Redis
$di->setShared('redis', function () use ($config) {
	$redis = new Redis();
	$redis->connect($config->redis->host, $config->redis->port, $config->redis->timeout);
	$redis->prefix = $config->redis->prefix;
	return $redis;
});
// 服务
$di->setShared('s', function () {
	return new SRVX\Service;
});
// 事务
$di->setShared('transactions', function (){
    return new TxManager();
});
