CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\MVC;
 10 
 11 use Cross\Cache\Driver\RedisDriver;
 12 use Cross\Exception\CoreException;
 13 use Cross\DB\Drivers\PDOSqlDriver;
 14 use Cross\DB\Drivers\CouchDriver;
 15 use Cross\DB\Drivers\MongoDriver;
 16 use Cross\DB\DBFactory;
 17 use Cross\Core\FrameBase;
 18 use Cross\Core\Config;
 19 use Cross\Http\Response;
 20 use Cross\Http\Request;
 21 
 22 /**
 23  * @author wonli <wonli@live.com>
 24  * Class Module
 25  * @package Cross\MVC
 26  * @property RedisDriver|CouchDriver|MongoDriver|PDOSqlDriver $link
 27  */
 28 class Module extends FrameBase
 29 {
 30     /**
 31      * 数据库连接的model名称
 32      *
 33      * @var string
 34      */
 35     private $linkName;
 36 
 37     /**
 38      * 数据库连接model类型
 39      *
 40      * @var string
 41      */
 42     private $linkType;
 43 
 44     /**
 45      * 数据库连接的model配置
 46      *
 47      * @var array
 48      */
 49     private $linkConfig;
 50 
 51     /**
 52      * 连接配置文件名
 53      * <pre>
 54      * 默认为项目目录下的config/db.config.php
 55      * 可以在app目录下init.php文件中通过'sys' => 'db_config'指定
 56      * </pre>
 57      *
 58      * @var string
 59      */
 60     protected $db_config_file;
 61 
 62     /**
 63      * 解析要连接model的参数
 64      *
 65      * @param string $params 指定要连接的数据库和配置项的key, 如mysql['db']这里的params应该为mysql:db
 66      * @throws CoreException
 67      */
 68     function __construct($params = '')
 69     {
 70         parent::__construct();
 71 
 72         $config = $this->parseModelParams($params);
 73         $this->linkName = &$config['model_name'];
 74         $this->linkType = &$config['model_type'];
 75         $this->linkConfig = &$config['model_config'];
 76     }
 77 
 78     /**
 79      * 创建model实例,参数格式和构造函数一致
 80      *
 81      * @param string $params
 82      * @param array $config
 83      * @return RedisDriver|CouchDriver|MongoDriver|PDOSqlDriver|mixed
 84      * @throws CoreException
 85      */
 86     function getModel($params = '', &$config = array())
 87     {
 88         static $cache = array();
 89         if (!isset($cache[$params])) {
 90             $config = $this->parseModelParams($params);
 91             $model = DBFactory::make($config['model_type'], $config['model_config'], array($this->getConfig()));
 92             $cache[$params] = array('config' => $config, 'model' => $model);
 93         } else {
 94             $model = $cache[$params]['model'];
 95             $config = $cache[$params]['config'];
 96         }
 97 
 98         return $model;
 99     }
100 
101     /**
102      * 当前link的model名称
103      *
104      * @return string
105      */
106     function getLinkName()
107     {
108         return $this->linkName;
109     }
110 
111     /**
112      * 当前link的model类型
113      *
114      * @return string
115      */
116     function getLinkType()
117     {
118         return $this->linkType;
119     }
120 
121     /**
122      * 当前link的model详细配置信息
123      *
124      * @return array
125      */
126     function getLinkConfig()
127     {
128         return $this->linkConfig;
129     }
130 
131     /**
132      * 获取带配置前缀的表名
133      *
134      * @param string $table
135      * @return string
136      */
137     function getPrefix($table = '')
138     {
139         return $this->link->getPrefix() . $table;
140     }
141 
142     /**
143      * 读取并解析数据库配置
144      *
145      * @return Config
146      * @throws CoreException
147      */
148     protected function databaseConfig()
149     {
150         static $database_config = null;
151         if (null === $database_config) {
152             $database_config = parent::loadConfig($this->getModuleConfigFile());
153         }
154 
155         return $database_config;
156     }
157 
158     /**
159      * 设置配置文件名
160      *
161      * @param $link_config_file
162      */
163     protected function setDatabaseConfigFile($link_config_file)
164     {
165         $this->db_config_file = $link_config_file;
166     }
167 
168     /**
169      * 解析指定model的类型和参数
170      *
171      * @param string $params
172      * @return array
173      * @throws CoreException
174      */
175     protected function parseModelParams($params = '')
176     {
177         $db_config_params = '';
178         if ($params) {
179             $db_config_params = $params;
180         } else {
181             static $default_db_config = '';
182             if ($default_db_config === '') {
183                 $default_db_config = $this->getConfig()->get('sys', 'default_db');
184             }
185 
186             if ($default_db_config) {
187                 $db_config_params = $default_db_config;
188             }
189         }
190 
191         if ($db_config_params) {
192             if (strpos($db_config_params, ':') === false) {
193                 throw new CoreException("数据库参数配置格式不正确: {$db_config_params}");
194             }
195 
196             list($model_type, $model_name) = explode(':', $db_config_params);
197         } else {
198             $model_name = 'db';
199             $model_type = 'mysql';
200         }
201 
202         static $cache;
203         if (!isset($cache[$model_type][$model_name])) {
204             $databaseConfig = $this->databaseConfig();
205             $model_config = $databaseConfig->get($model_type, $model_name);
206             if (empty($model_config)) {
207                 throw new CoreException("未配置的Model: {$model_type}:{$model_name}");
208             }
209 
210             $cache[$model_type][$model_name] = array(
211                 'model_name' => $model_name,
212                 'model_type' => $model_type,
213                 'model_config' => $model_config,
214             );
215         }
216 
217         return $cache[$model_type][$model_name];
218     }
219 
220     /**
221      * 获取默认model的实例
222      *
223      * @return RedisDriver|CouchDriver|MongoDriver|PDOSqlDriver|mixed
224      * @throws CoreException
225      */
226     private function getLink()
227     {
228         return DBFactory::make($this->linkType, $this->linkConfig, array($this->getConfig()));
229     }
230 
231     /**
232      * 获取连接配置文件名
233      *
234      * @return mixed
235      */
236     private function getModuleConfigFile()
237     {
238         if (!$this->db_config_file) {
239             $db_config_file = $this->getConfig()->get('sys', 'db_config');
240             if (!$db_config_file) {
241                 $db_config_file = 'db.config.php';
242             }
243 
244             $this->setDatabaseConfigFile($db_config_file);
245         }
246 
247         return $this->db_config_file;
248     }
249 
250     /**
251      * 访问link属性时才实例化model
252      *
253      * @param string $property
254      * @return RedisDriver|Config|CouchDriver|MongoDriver|PDOSqlDriver|Request|Response|View|mixed|null
255      * @throws CoreException
256      */
257     function __get($property)
258     {
259         switch ($property) {
260             case 'link' :
261                 return $this->link = $this->getLink();
262 
263             default :
264                 return parent::__get($property);
265         }
266     }
267 }
268 
CrossPHP API documentation generated by ApiGen