/** * Display a listing of the resource. * * @return Response */ public function index() { // Using factory // Getting an instance of a model User example $user = ModelFactory::getInstance('User'); // Getting an instance of a Library String $stringLib = LibraryFactory::getInstance('String'); // Getting an instance of a Filter DateRange $dateRange = FilterFactory::getInstance('DateRange'); // Getting an instance of a Type User $userType = TypeFactory::getInstance('User'); // Perform an access check AccessCheckFactory::getInstance('User')->canAccess(auth()->user()->id, 'view'); // Using facade // Getting an instance of a model User example $user = \Model::getInstance('User'); // Getting an instance of a Library String $stringLib = \Library::getInstance('String'); // Getting an instance of a Filter DateRange $dateRange = \Filter::getInstance('DateRange'); // Getting an instance of a Type User $userType = \Type::getInstance('User'); // Passing data to view example $this->view->fullname = auth()->user()->fullname; // Perform an access check \AccessCheck::getInstance('User')->canAccess(1, 'view'); return $this->view('dashboard'); }
* 格式化并补全属性定义数组 * * @param array $attribute * @return array */ public static function normalizeAttribute(array $attribute) { $defaults = ['allow_null' => false, 'allow_tags' => false, 'auto_generate' => false, 'default' => null, 'deprecated' => false, 'pattern' => null, 'primary_key' => false, 'protected' => false, 'refuse_update' => false, 'strict' => null, 'type' => null]; $type = isset($attribute['type']) ? $attribute['type'] : null; $attribute = array_merge($defaults, self::factory($type)->normalizeAttribute($attribute)); if ($attribute['allow_null']) { $attribute['default'] = null; } if ($attribute['primary_key']) { $attribute['allow_null'] = false; $attribute['refuse_update'] = true; $attribute['strict'] = true; } if ($attribute['protected'] && $attribute['strict'] === null) { $attribute['strict'] = true; } return $attribute; } private static $instance; public static function getInstance() { return self::$instance ?: (self::$instance = new self()); } } Type::getInstance()->register('common', '\\Owl\\DataMapper\\Type\\Common')->register('datetime', '\\Owl\\DataMapper\\Type\\Datetime')->register('integer', '\\Owl\\DataMapper\\Type\\Integer')->register('json', '\\Owl\\DataMapper\\Type\\Json')->register('number', '\\Owl\\DataMapper\\Type\\Number')->register('pg_array', '\\Owl\\DataMapper\\Type\\PgsqlArray')->register('pg_hstore', '\\Owl\\DataMapper\\Type\\PgsqlHstore')->register('string', '\\Owl\\DataMapper\\Type\\Text')->register('uuid', '\\Owl\\DataMapper\\Type\\UUID')->register('complex', '\\Owl\\DataMapper\\Type\\Complex');
/** * 把存储服务内获取的数据,打包成Data实例. * * @param array $record * @param Data [$data] * * @return Data */ public function pack(array $record, Data $data = null) { $types = Type::getInstance(); $values = []; $attributes = $this->getAttributes(); foreach ($record as $key => $value) { if (!isset($attributes[$key])) { continue; } $attribute = $attributes[$key]; $values[$key] = $types->get($attribute['type'])->restore($value, $attribute); } if ($data) { $data->__pack($values, false); } else { $class = $this->class; $data = new $class(null, ['fresh' => false]); $data->__pack($values, true); } return $data; }
* 格式化并补全属性定义数组 * * @param array $attribute * @return array */ public static function normalizeAttribute(array $attribute) { $defaults = ['allow_null' => false, 'auto_generate' => false, 'default' => null, 'deprecated' => false, 'pattern' => null, 'primary_key' => false, 'protected' => false, 'refuse_update' => false, 'strict' => null, 'type' => null]; $type = isset($attribute['type']) ? $attribute['type'] : null; $attribute = array_merge($defaults, self::factory($type)->normalizeAttribute($attribute)); if ($attribute['allow_null']) { $attribute['default'] = null; } if ($attribute['primary_key']) { $attribute['allow_null'] = false; $attribute['refuse_update'] = true; $attribute['strict'] = true; } if ($attribute['protected'] && $attribute['strict'] === null) { $attribute['strict'] = true; } return $attribute; } private static $instance; public static function getInstance() { return self::$instance ?: (self::$instance = new self()); } } Type::getInstance()->register('mixed', '\\Owl\\DataMapper\\Type\\Mixed')->register('datetime', '\\Owl\\DataMapper\\Type\\Datetime')->register('integer', '\\Owl\\DataMapper\\Type\\Integer')->register('json', '\\Owl\\DataMapper\\Type\\Json')->register('numeric', '\\Owl\\DataMapper\\Type\\Numeric')->register('pg_array', '\\Owl\\DataMapper\\Type\\PgsqlArray')->register('pg_hstore', '\\Owl\\DataMapper\\Type\\PgsqlHstore')->register('string', '\\Owl\\DataMapper\\Type\\String')->register('uuid', '\\Owl\\DataMapper\\Type\\UUID');