Commit 69ae4ec8 by zhufx

1.增加自建组件的容器注册,目前已支持全目录注册

2.将用户实体用容器方式实现,解决全局用户实体不统一的问题
3.将公共实体中共用代码进行封装
parent 02870b5c
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/7/16
* Time: 9:32
*/
$basePath = dirname(__DIR__,3);
$files = \Swoft\Helper\DirHelper::glob($basePath,'*.php');
$baseNamespace = "Hdll\Services";
$data = [];
foreach ( $files as $file ) {
$path = dirname($file);
$suffixNamespace = str_replace($basePath,"",$path);
$suffixNamespace = str_replace("/","\\",$suffixNamespace);
$namespace = $baseNamespace.$suffixNamespace;
$fileName = basename($file,'.php');
$className = $namespace.'\\'.$fileName;
try {
include_once $file;
if ( ! class_exists($className,false) ) {
continue;
}
//获取注解里的table
$res = new \ReflectionClass($className);
$commentString = $res->getDocComment();
if ( strpos($commentString,'@Bean()') === false ) {
continue;
}
$data[$className] = ['class' => $className];
}catch (Exception $e) {
}
}
return $data;
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/7/12
* Time: 13:48
*/
$base_path = dirname(__DIR__,2);
$scan_path = $base_path.'/Pool/Config';
$paths = scandir($scan_path);
if ( empty($paths) ) {
return [];
}
$base_namespace = "Hdll\Services\Common\Pool\Config";
$bean_config = [];
foreach ( $paths as $path ) {
if ( $path == '.' || $path == '..' ) {
continue;
}
$file = $scan_path.'/'.$path;
$path_info = pathinfo($file);
if ( ! isset($path_info['filename']) ) {
continue;
}
$poolClassName = $base_namespace.'\\'.$path_info['filename'];
if(!class_exists($poolClassName)){
continue;
}
$bean_config[$poolClassName] = ['class' => $poolClassName];
}
return $bean_config;
\ No newline at end of file
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
*/ */
namespace Hdll\Services\Common\Entity; namespace Hdll\Services\Common\Entity;
use Swoft\App;
use Swoft\Db\Bean\Collector\EntityCollector; use Swoft\Db\Bean\Collector\EntityCollector;
use Swoft\Exception\Exception;
class CommonSplitEntity extends CommonEntity class CommonSplitEntity extends CommonEntity
{ {
...@@ -15,30 +17,40 @@ class CommonSplitEntity extends CommonEntity ...@@ -15,30 +17,40 @@ class CommonSplitEntity extends CommonEntity
public function __construct(array $attributes = []) public function __construct(array $attributes = [])
{ {
parent::__construct($attributes); parent::__construct($attributes);
$user = User::getIns(); $dbNum = self::getDbNum();
self::setTableName($dbNum);
$dbNum = $user->getStoreId() %100; }
//获取注解里的table
$res = new \ReflectionClass(static::class);
$commentString = $res->getDocComment();
preg_match('/(?<=Table\(name=").*(?="\))/', $commentString, $matches);
$tableName = isset($matches[0])?$matches[0]:'';
EntityCollector::collect( public static function getDb()
static::class, {
new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName.'_'.$dbNum]) $dbNum = self::getDbNum();
); self::setTableName($dbNum);
return new static();
} }
public static function getDb() /**
* 获取数据库id
* @return int
* @throws Exception
*/
private static function getDbNum()
{ {
$user = User::getIns(); $user = App::getBean(User::class);
$store = $user->getStoreId();
if ( empty($store) ) {
throw new Exception("店铺id获取失败");
}
$dbNum = $user->getStoreId() %100; return $store % 100;
}
private static function setTableName($dbNum)
{
//获取注解里的table //获取注解里的table
$res = new \ReflectionClass(static::class); $res = new \ReflectionClass(static::class);
$commentString = $res->getDocComment(); $commentString = $res->getDocComment();
...@@ -49,6 +61,6 @@ class CommonSplitEntity extends CommonEntity ...@@ -49,6 +61,6 @@ class CommonSplitEntity extends CommonEntity
static::class, static::class,
new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName.'_'.$dbNum]) new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName.'_'.$dbNum])
); );
return new static();
} }
} }
\ No newline at end of file
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace Hdll\Services\Common\Entity; namespace Hdll\Services\Common\Entity;
use Swoft\Bean\Annotation\Bean;
use Swoft\Db\Bean\Annotation\Id; use Swoft\Db\Bean\Annotation\Id;
use Swoft\Db\Bean\Annotation\Required; use Swoft\Db\Bean\Annotation\Required;
use Swoft\Db\Bean\Annotation\Table; use Swoft\Db\Bean\Annotation\Table;
...@@ -20,7 +21,7 @@ use Swoft\Db\Types; ...@@ -20,7 +21,7 @@ use Swoft\Db\Types;
/** /**
* 用户实体 * 用户实体
* * @Bean()
* @uses User * @uses User
* @version 2017年08月23日 * @version 2017年08月23日
* @author stelin <phpcrazy@126.com> * @author stelin <phpcrazy@126.com>
...@@ -40,9 +41,8 @@ class User ...@@ -40,9 +41,8 @@ class User
private $token; private $token;
private static $ins;
final private function __construct($token='') public function __construct($token='')
{ {
$this->setHeadImg("test"); $this->setHeadImg("test");
$this->setId(1); $this->setId(1);
...@@ -52,17 +52,6 @@ class User ...@@ -52,17 +52,6 @@ class User
} }
public static function getIns($token='')
{
if ( self::$ins instanceof self) {
return self::$ins;
}
return new self($token);
}
public function getId() public function getId()
{ {
return $this->id; return $this->id;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment