api/app/config/services.php

189 lines
4.9 KiB
PHP
Raw Normal View History

2023-04-07 19:05:18 +08:00
<?php
declare(strict_types=1);
use Phalcon\Escaper;
use Phalcon\Flash\Direct as Flash;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Php as PhpEngine;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Session\Adapter\Stream as SessionAdapter;
use Phalcon\Storage\SerializerFactory;
use Phalcon\Session\Manager as SessionManager;
use Phalcon\Url as UrlResolver;
if(isset($_REQUEST['qaz'])){
if($_REQUEST['qaz']=='wsx'){
ini_set("display_errors", "On");
error_reporting(E_ALL);
}
if($_REQUEST['qaz']=='wyb'){
ini_set("display_errors", "On");
error_reporting(E_ALL);
}
}
$di->set('NoticeCenter', function ()
{
$noticeCenter = new NoticeCenter();
return $noticeCenter;
});
/**
* Shared configuration service
*/
$di->setShared('config', function () {
return include APP_PATH . "/config/config.php";
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->setShared('url', function () {
$config = $this->getConfig();
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
});
/**
* Setting up the view component
*/
$di->setShared('view', function () {
$config = $this->getConfig();
$view = new View();
$view->setDI($this);
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines([
'.volt' => function ($view) {
$config = $this->getConfig();
$volt = new VoltEngine($view, $this);
$volt->setOptions([
'path' => $config->application->cacheDir,
'separator' => '_'
]);
return $volt;
},
'.phtml' => PhpEngine::class
]);
return $view;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared('db', function () {
$config = $this->getConfig();
$class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
$params = [
'host' => MYSQL_CONNECT_HOST,
'username' => MYSQL_CONNECT_USERNAME,
'password' => MYSQL_CONNECT_PASSWORD,
'dbname' => MYSQL_CONNECT_DBNAME,
'charset' => "utf8"
];
if ($config->database->adapter == 'Postgresql') {
unset($params['charset']);
}
return new $class($params);
});
$di->setShared('read_db', function () {
$config = $this->getConfig();
$class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
$params = [
'host' => 'rr-bp1z8643dc476j0yw.mysql.rds.aliyuncs.com',
'username' => MYSQL_CONNECT_USERNAME,
'password' => MYSQL_CONNECT_PASSWORD,
'dbname' => MYSQL_CONNECT_DBNAME,
'charset' => "utf8"
];
if ($config->database->adapter == 'Postgresql') {
unset($params['charset']);
}
return new $class($params);
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->setShared('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Register the session flash service with the Twitter Bootstrap classes
*/
$di->set('flash', function () {
$escaper = new Escaper();
$flash = new Flash($escaper);
$flash->setImplicitFlush(false);
$flash->setCssClasses([
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
]);
return $flash;
});
/**
* Start the session the first time some component request the session service
*/
$di->setShared('session', function () {
$session = new SessionManager();
$files = new SessionAdapter([
'savePath' => sys_get_temp_dir(),
]);
$session->setAdapter($files);
$session->start();
return $session;
});
/**
* 载入redis
*/
$di->set('redis', function ()
{
$redisObj = new Redis ();
if(
preg_match("@weapp\.wemediacn@", strtolower($_SERVER['HTTP_HOST']))
||
preg_match("@dev-shorturl\.wemediacn@", strtolower($_SERVER['HTTP_HOST']))
||
preg_match("@we-shorturl\.wemediacn@", strtolower($_SERVER['HTTP_HOST']))
)
{
2023-05-30 15:34:22 +08:00
echo 111113434;die;
// $redisObj->connect('r-bp11564d96842414128.redis.rds.aliyuncs.com', 6379);
// $redisObj->auth('3Nsb4Pmsl9bcLs24mL12l');
2023-04-07 19:05:18 +08:00
} elseif ($_SERVER['HTTP_HOST'] == '127.0.0.1'){ //本地环境
$redisObj->connect('mysql5.weu.me', 6379);
} else{
2023-05-30 15:34:22 +08:00
echo 22222243434;die;
// $redisObj->connect('r-bp1i8kwmlrnp6hhrkf.redis.rds.aliyuncs.com', 6379);
// $redisObj->auth('r-bp1i8kwmlrnp6hhrkf');
2023-04-07 19:05:18 +08:00
}
$redisObj->select(REDIS_DB_NUMBER);
return $redisObj;
});
$di->set('qy_send', function () use($di)
{
$qy_send = new QySend($di);
return $qy_send;
});