/**
  * Generates the bit value for the bundle's permission
  *
  * @param array $permissions
  *
  * @return array
  * @throws \InvalidArgumentException
  */
 public function generatePermissions(array $permissions)
 {
     $entities = array();
     //give bundles an opportunity to analyze and adjust permissions based on others
     $classes = $this->getPermissionObjects();
     //bust out permissions into their respective bundles
     $bundlePermissions = array();
     foreach ($permissions as $permission => $perms) {
         list($bundle, $level) = explode(':', $permission);
         $bundlePermissions[$bundle][$level] = $perms;
     }
     $bundles = array_keys($classes);
     foreach ($bundles as $bundle) {
         if (!isset($bundlePermissions[$bundle])) {
             $bundlePermissions[$bundle] = array();
         }
     }
     //do a first round to give bundles a chance to update everything and give an opportunity to require a second round
     //if the permission it is looking for from another bundle is not configured yet
     $secondRound = array();
     foreach ($classes as $bundle => $class) {
         $needsRoundTwo = $class->analyzePermissions($bundlePermissions[$bundle], $bundlePermissions);
         if ($needsRoundTwo) {
             $secondRound[] = $bundle;
         }
     }
     foreach ($secondRound as $bundle) {
         $classes[$bundle]->analyzePermissions($bundlePermissions[$bundle], $bundlePermissions, true);
     }
     //get a list of plugin bundles so we can tell later if a bundle is core or plugin
     $pluginBundles = $this->getPluginBundles();
     //create entities
     foreach ($bundlePermissions as $bundle => $permissions) {
         foreach ($permissions as $name => $perms) {
             $entity = new Permission();
             //strtolower to ensure consistency
             $entity->setBundle(strtolower($bundle));
             $entity->setName(strtolower($name));
             $bit = 0;
             $class = $this->getPermissionObject($bundle, true, array_key_exists(ucfirst($bundle) . "Bundle", $pluginBundles));
             foreach ($perms as $perm) {
                 //get the bit for the perm
                 if (!$class->isSupported($name, $perm)) {
                     throw new \InvalidArgumentException("{$perm} does not exist for {$bundle}:{$name}");
                 }
                 $bit += $class->getValue($name, $perm);
             }
             $entity->setBitwise($bit);
             $entities[] = $entity;
         }
     }
     return $entities;
 }
 /**
  * {@inheritDoc}
  */
 public function setBundle($bundle)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setBundle', array($bundle));
     return parent::setBundle($bundle);
 }