Code Coverage for /home/user/workspace/all/Pyrus/src/Pyrus/Installer/Role.php

Coverage: 71%

Aggregate Code Coverage for all tests

       1           : <?php
       2           : /**
       3           :  * PEAR2_Pyrus_Installer_Role
       4           :  *
       5           :  * PHP version 5
       6           :  *
       7           :  * @category  PEAR2
       8           :  * @package   PEAR2_Pyrus
       9           :  * @author    Greg Beaver <cellog@php.net>
      10           :  * @copyright 2008 The PEAR Group
      11           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      12           :  * @version   SVN: $Id$
      13           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      14           :  */
      15           : 
      16           : /**
      17           :  * Base class for installation roles for files.
      18           :  *
      19           :  * @category  PEAR2
      20           :  * @package   PEAR2_Pyrus
      21           :  * @author    Greg Beaver <cellog@php.net>
      22           :  * @copyright 2008 The PEAR Group
      23           :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      24           :  * @link      http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/
      25           :  */
      26           : class PEAR2_Pyrus_Installer_Role
      27         2 : {
      28           :     static private $_roles;
      29           :     /**
      30           :      * Set up any additional configuration variables that file roles require
      31           :      *
      32           :      * Never call this directly, it is called by the PEAR_Config constructor
      33           :      * @param PEAR2_Pyrus_Config
      34           :      * @access private
      35           :      * @static
      36           :      */
      37           :     public static function initializeConfig(PEAR2_Pyrus_Config $config)
      38           :     {
      39           :         if (!isset(self::$_roles)) {
      40           :             self::registerRoles();
      41           :         }
      42           : 
      43           :         foreach (self::$_roles as $class => $info) {
      44           :             if (!$info['config_vars']) {
      45           :                 continue;
      46           :             }
      47           : 
      48           :             $config->addConfigValue($info['config_vars']);
      49           :         }
      50           :     }
      51           : 
      52           :     /**
      53           :      * @param PEAR2_Pyrus_PackageFile_v2
      54           :      * @param string role name
      55           :      * @param PEAR2_Pyrus_Config
      56           :      * @return PEAR2_Pyrus_Installer_Role_Common
      57           :      * @static
      58           :      */
      59           :     static function factory($pkg, $role)
      60           :     {
      61         2 :         if (!isset(self::$_roles)) {
      62           :             self::registerRoles();
      63           :         }
      64           : 
      65         2 :         if (!in_array($role, self::getValidRoles($pkg->getPackageType()))) {
      66           :             return $a;
      67           :         }
      68           : 
      69         2 :         $a = 'PEAR2_Pyrus_Installer_Role_' . ucfirst($role);
      70         2 :         return new $a(PEAR2_Pyrus_Config::current());
      71           :     }
      72           : 
      73           :     /**
      74           :      * Get a list of file roles that are valid for the particular release type.
      75           :      *
      76           :      * For instance, src files serve no purpose in regular php releases.
      77           :      * @param string
      78           :      * @param bool clear cache
      79           :      * @return array
      80           :      * @static
      81           :      */
      82           :     static function getValidRoles($release, $clear = false)
      83           :     {
      84         2 :         if (!isset(self::$_roles)) {
      85         2 :             self::registerRoles();
      86         2 :         }
      87           : 
      88         2 :         static $ret = array();
      89         2 :         if ($clear) {
      90         2 :             $ret = array();
      91         2 :         }
      92           : 
      93         2 :         if (isset($ret[$release])) {
      94         2 :             return $ret[$release];
      95           :         }
      96           : 
      97         2 :         $ret[$release] = array();
      98         2 :         foreach (self::$_roles as $role => $okreleases) {
      99         2 :             if (in_array($release, $okreleases['releasetypes'])) {
     100         2 :                 $ret[$release][] = strtolower(str_replace('PEAR2_Pyrus_Installer_Role_', '', $role));
     101         2 :             }
     102         2 :         }
     103           : 
     104         2 :         return $ret[$release];
     105           :     }
     106           : 
     107           :     /**
     108           :      * Get a list of roles that require their files to be installed
     109           :      *
     110           :      * Most roles must be installed, but src and package roles, for instance
     111           :      * are pseudo-roles.  src files are compiled into a new extension.  Package
     112           :      * roles are actually fully bundled releases of a package
     113           :      * @param bool clear cache
     114           :      * @return array
     115           :      * @static
     116           :      */
     117           :     static function getInstallableRoles($clear = false)
     118           :     {
     119         2 :         if (!isset(self::$_roles)) {
     120           :             self::registerRoles();
     121           :         }
     122           : 
     123         2 :         static $ret;
     124         2 :         if ($clear) {
     125         2 :             unset($ret);
     126         2 :         }
     127           : 
     128         2 :         if (!isset($ret)) {
     129         2 :             $ret = array();
     130         2 :             foreach (self::$_roles as $role => $okreleases) {
     131         2 :                 if ($okreleases['installable']) {
     132         2 :                     $ret[] = strtolower(str_replace('PEAR2_Pyrus_Installer_Role_', '', $role));
     133         2 :                 }
     134         2 :             }
     135         2 :         }
     136           : 
     137         2 :         return $ret;
     138           :     }
     139           : 
     140           :     /**
     141           :      * Return an array of roles that are affected by the baseinstalldir attribute
     142           :      *
     143           :      * Most roles ignore this attribute, and instead install directly into:
     144           :      * PackageName/filepath
     145           :      * so a tests file tests/file.phpt is installed into PackageName/tests/filepath.php
     146           :      * @param bool clear cache
     147           :      * @return array
     148           :      * @static
     149           :      */
     150           :     static function getBaseinstallRoles($clear = false)
     151           :     {
     152         2 :         if (!isset(self::$_roles)) {
     153           :             self::registerRoles();
     154           :         }
     155           : 
     156         2 :         static $ret;
     157         2 :         if ($clear) {
     158         2 :             unset($ret);
     159         2 :         }
     160           : 
     161         2 :         if (!isset($ret)) {
     162         2 :             $ret = array();
     163         2 :             foreach (self::$_roles as $role => $okreleases) {
     164         2 :                 if ($okreleases['honorsbaseinstall']) {
     165         2 :                     $ret[] = strtolower(str_replace('PEAR2_Pyrus_Installer_Role_', '', $role));
     166         2 :                 }
     167         2 :             }
     168         2 :         }
     169           : 
     170         2 :         return $ret;
     171           :     }
     172           : 
     173           :     /**
     174           :      * Return an array of file roles that should be analyzed for PHP content at package time,
     175           :      * like the "php" role.
     176           :      * @param bool clear cache
     177           :      * @return array
     178           :      * @static
     179           :      */
     180           :     static function getPhpRoles($clear = false)
     181           :     {
     182         2 :         if (!isset(self::$_roles)) {
     183           :             self::registerRoles();
     184           :         }
     185           : 
     186         2 :         static $ret;
     187         2 :         if ($clear) {
     188         2 :             unset($ret);
     189         2 :         }
     190           : 
     191         2 :         if (!isset($ret)) {
     192         2 :             $ret = array();
     193         2 :             foreach (self::$_roles as $role => $okreleases) {
     194         2 :                 if ($okreleases['phpfile']) {
     195         2 :                     $ret[] = strtolower(str_replace('PEAR2_Pyrus_Installer_Role_', '', $role));
     196         2 :                 }
     197         2 :             }
     198         2 :         }
     199           : 
     200         2 :         return $ret;
     201           :     }
     202           : 
     203           :     /**
     204           :      * Scan through the Command directory looking for classes
     205           :      * and see what commands they implement.
     206           :      * @param string which directory to look for classes, defaults to
     207           :      *               the Installer/Roles subdirectory of
     208           :      *               the directory from where this file (__FILE__) is
     209           :      *               included.
     210           :      *
     211           :      * @return bool TRUE on success, a PEAR error on failure
     212           :      * @access public
     213           :      * @static
     214           :      */
     215           :     static function registerRoles($dir = null)
     216           :     {
     217         2 :         self::$_roles = array();
     218         2 :         $parser = new PEAR2_Pyrus_XMLParser;
     219         2 :         if ($dir === null) {
     220         2 :             $dir = dirname(__FILE__) . '/Role';
     221         2 :         }
     222           : 
     223         2 :         if (!file_exists($dir) || !is_dir($dir)) {
     224           :             throw new PEAR2_Pyrus_Installer_Role_Exception("registerRoles: opendir($dir) failed");
     225           :         }
     226           : 
     227         2 :         $dp = @opendir($dir);
     228         2 :         if (empty($dp)) {
     229           :             throw new PEAR2_Pyrus_Installer_Role_Exception("registerRoles: opendir($dir) failed");
     230           :         }
     231           : 
     232         2 :         while ($entry = readdir($dp)) {
     233         2 :             if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
     234         2 :                 continue;
     235           :             }
     236           : 
     237         2 :             $class = "PEAR2_Pyrus_Installer_Role_".substr($entry, 0, -4);
     238           :             // List of roles
     239         2 :             if (!isset(self::$_roles[$class])) {
     240         2 :                 $file = "$dir/$entry";
     241         2 :                 $data = $parser->parse($file);
     242         2 :                 $data = $data['role'];
     243         2 :                 if (!is_array($data['releasetypes'])) {
     244           :                     $data['releasetypes'] = array($data['releasetypes']);
     245           :                 }
     246         2 :                 self::$_roles[$class] = $data;
     247         2 :             }
     248         2 :         }
     249           : 
     250         2 :         closedir($dp);
     251         2 :         $roles = self::$_roles;
     252         2 :         ksort($roles);
     253         2 :         self::$_roles = $roles;
     254         2 :         self::getBaseinstallRoles(true);
     255         2 :         self::getInstallableRoles(true);
     256         2 :         self::getPhpRoles(true);
     257         2 :         self::getValidRoles('****', true);
     258         2 :         return true;
     259           :     }
     260           : 
     261           :     /**
     262           :      * Retrieve configuration information about a file role from its XML info
     263           :      *
     264           :      * @param string $role Role Classname, as in "PEAR2_Pyrus_Installer_Role_Data"
     265           :      * @return array
     266           :      */
     267           :     static function getInfo($role)
     268           :     {
     269         2 :         if (!isset(self::$_roles)) {
     270           :             self::registerRoles();
     271           :         }
     272           : 
     273         2 :         if (empty(self::$_roles[$role])) {
     274           :             throw new PEAR2_Pyrus_Installer_Role_Exception('Unknown Role class: "' . $role . '"');
     275           :         }
     276           : 
     277         2 :         return self::$_roles[$role];
     278           :     }
     279         2 : }