コード例 #1
0
ファイル: Factory.php プロジェクト: qkrcjfgus33/xpressengine
 /**
  * Make permission instances by type
  *
  * @param string                $type    type string
  * @param MemberEntityInterface $user    user instance
  * @param string                $siteKey site key
  * @return Permission[]
  * @throws NotMatchedInstanceException
  */
 public function makesByType($type, MemberEntityInterface $user = null, $siteKey = 'default')
 {
     $user = $user ?: $this->auth->user();
     $permissions = [];
     $registereds = $this->repo->fetchByType($siteKey, $type);
     foreach ($registereds as $registered) {
         $ancestors = array_filter($registereds, function ($item) use($registered) {
             $itemNames = explode('.', $item->name);
             $registeredNames = explode('.', $registered->name);
             if (count($itemNames) >= count($registeredNames)) {
                 return false;
             }
             for ($i = 0; $i < count($itemNames); $i++) {
                 if ($itemNames[$i] !== $registeredNames[$i]) {
                     return false;
                 }
             }
             return true;
         });
         if (count($ancestors) > 0) {
             uasort($ancestors, [$this, 'cmp']);
         }
         foreach ($ancestors as $ancestor) {
             $registered->addParent($ancestor);
         }
         if (isset($this->extends[$type]) === true) {
             $permission = $this->extends[$type]($registered->name, $user, $registered);
             if ($permission instanceof Permission === false) {
                 throw new NotMatchedInstanceException(['type' => $type]);
             }
             $permissions[$registered->name] = $permission;
         } else {
             switch ($type) {
                 case 'route':
                     $route = $this->routes->getByName($registered->name);
                     $permissions[$registered->name] = new RoutePermission($route, $user, $registered);
                     break;
                 case 'instance':
                     $permissions[$registered->name] = new InstancePermission($registered->name, $user, $registered);
                     break;
             }
         }
     }
     return $permissions;
 }