/** * Get full list of installed packages * * @return array of phpRack_Adapters_Pear_Package * @throws phpRack_Exception If some problem appear during package informations loading * @see phpRack_Package_Pear::showList() */ public function getAllPackages() { $packages = array(); $command = 'pear list -a'; /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; $result = phpRack_Adapters_Shell_Command::factory($command)->run(); // divide command output by channels foreach (explode("\n\n", $result) as $channel) { $lines = explode("\n", $channel); $matches = array(); // get channel name if (!preg_match('/CHANNEL ([^:]+):/', $lines[0], $matches)) { continue; } $channelName = $matches[1]; // skip 3 first lines (channel, separator, packages header line) $packageLines = array_slice($lines, 3); foreach ($packageLines as $packageLine) { // get package name if (preg_match('/^(\\w+)/', $packageLine, $matches)) { // set full package name with channel $packageName = "{$channelName}/{$matches[1]}"; $packages[] = new phpRack_Adapters_Pear_Package($packageName); } } } return $packages; }
/** * Construct the class * * @param string Name of the package * @throws Exception if PEAR is not installed properly * @return void */ public function __construct($name) { $this->_name = $name; $command = 'pear info ' . escapeshellarg($this->_name); /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; $result = phpRack_Adapters_Shell_Command::factory($command)->run(); if (!$result) { throw new Exception('PEAR is not installed properly'); } $this->_rawInfo = $result; }
/** * Execute a command and tries to find a regex inside it's result * * Use it like this, to make sure that PHP scripts are started * by "apache" user: * * <code> * class MyTest extends phpRack_Test { * public function testAuthorship() { * $this->assert->shell->exec('whoami', '/apache/'); * } * } * </code> * * @param string Command to run * @param string Regular exception * @return $this */ public function exec($cmd, $regex = null) { /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; $result = phpRack_Adapters_Shell_Command::factory($cmd)->run(); $this->_log('$ ' . $cmd); $this->_log($result); if (!is_null($regex)) { if (!preg_match($regex, $result)) { $this->_failure("Result of '{$cmd}' doesn't match regex '{$regex}': '{$result}'"); } else { $this->_success("Result of '{$cmd}' matches regex '{$regex}'"); } } return $this; }
/** * Get CPU frequency in MHz * * @return float * @throws Exception If can't get cpu frequency * @see getBogoMips() * @see phpRack_Adapters_Cpu_Abstract::getCpuFrequency() */ public function getCpuFrequency() { /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; $data = phpRack_Adapters_Shell_Command::factory('system_profiler SPHardwareDataType -xml 2>&1')->run(); $xml = simplexml_load_string($data); if (!$xml instanceof SimpleXMLElement) { throw new Exception("Invalid result from system_profiler: '{$data}'"); } $nodes = $xml->xpath('//string[preceding-sibling::key="current_processor_speed"]'); $node = strval($nodes[0]); if (strpos($node, 'GHz') === false) { throw new Exception("Strange frequency from system_profiler: '{$node}'"); } return floatval($node) * 1000; }
/** * Get result of "cat /proc/cpuinfo" shell command execution * * @return string * @throws phpRack_Exception If unable to execute shell command * @see getBogoMips() * @see getCpuFrequency() */ private function _getCpuInfoData() { $command = 'cat /proc/cpuinfo'; /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; // Exception is possible here $result = phpRack_Adapters_Shell_Command::factory($command)->run(); return $result; }
/** * Check files in directory have correct php syntax. * * Possible options are: * * <code> * class MyTest extends phpRack_Test { * public function testCodeValidity() { * $this->assert->php->lint( * '/home/myproject/php', // path to PHP files * array( * 'extensions' => 'php,phtml', // comma-separated list of extensions to parse * 'exclude' => array('/\.svn/'), // list of RegExps to exclude * 'verbose' => true, // show detailed log, with one file per line * ) * ); * } * } * </code> * * @param string Directory path to check * @param array List of options * @return $this */ public function lint($dir, array $options = array()) { require_once PHPRACK_PATH . '/Adapters/File.php'; $dir = phpRack_Adapters_File::factory($dir)->getFileName(); if (!file_exists($dir)) { $this->_failure("Directory '{$dir}' does not exist"); return $this; } // Create our file iterator require_once PHPRACK_PATH . '/Adapters/Files/DirectoryFilterIterator.php'; $iterator = phpRack_Adapters_Files_DirectoryFilterIterator::factory($dir); if (!empty($options['exclude'])) { $iterator->setExclude($options['exclude']); } if (!empty($options['extensions'])) { $iterator->setExtensions($options['extensions']); } $lintCommand = 'php -l'; $valid = $invalid = 0; foreach ($iterator as $file) { $file = realpath($file->getPathname()); $command = $lintCommand . ' ' . escapeshellarg($file) . ' 2>&1'; /** * @see phpRack_Adapters_Shell_Command */ require_once PHPRACK_PATH . '/Adapters/Shell/Command.php'; $output = phpRack_Adapters_Shell_Command::factory($command)->run(); if (preg_match('#^No syntax errors detected#', $output)) { if (!empty($options['verbose'])) { $this->_success("File '{$file}' is valid"); } $valid++; } else { $this->_failure("File '{$file}' is NOT valid:"); $this->_log($output); $invalid++; } } // notify phpRack about success in the test if (!$invalid) { $this->_success("{$valid} files are LINT-valid"); } $this->_log(sprintf('%d files LINT-checked, among them: %d valid and %d invalid', $valid + $invalid, $valid, $invalid)); return $this; }