Пример #1
0
 public function test_ucfirst_and_capitalize()
 {
     $this->assertEquals(\Sledgehammer\text('bob')->ucfirst(), 'Bob');
     $this->assertEquals(\Sledgehammer\text('STOP')->ucfirst(), 'STOP', 'ucfirst() should do nothing when the first chararakter already is uppercase');
     $this->assertEquals(\Sledgehammer\text('STOP')->capitalize(), 'Stop', 'capitalize() should convert the first charakter to uppercase and the rest to lowercase');
 }
Пример #2
0
 /**
  * Download and install a PEAR package.
  *
  * @throws Exceptions on failure
  *
  * @param string $package
  * @param string $version
  * @param array  $options array(
  *                        'version' = Install a specific version
  *                        'target' => alternative target directory
  *                        'channel' => specifiy the channel
  *                        )
  */
 public function install($package, $options = [])
 {
     $version = \Sledgehammer\array_value($options, 'version') ?: 'stable';
     if (isset($options['channel'])) {
         $channel = $options['channel'];
         $this->addChannel($channel);
         if (empty($this->channels[$channel]['packages'][$package])) {
             if (isset($this->channels[$channel]['packages'])) {
                 foreach ($this->channels[$channel]['packages'] as $name => $info) {
                     if (strcasecmp($name, $package) === 0) {
                         return $this->install($name, $options);
                     }
                 }
             }
             throw new InfoException('Package "' . $package . '" not found in channel: ' . $channel, \Sledgehammer\quoted_human_implode(' and ', array_keys($this->channels[$channel]['packages'])));
         }
         $packageLocation =& $this->channels[$channel]['packages'][$package];
     } else {
         if (count($this->channels) === 0) {
             $this->addChannel('pear.php.net');
         }
         if (empty($this->packages[$package])) {
             foreach ($this->packages as $name => $channel) {
                 if (strcasecmp($name, $package) === 0) {
                     return $this->install($name, $options);
                 }
             }
             throw new InfoException('Package "' . $package . '" not found in channels: ' . \Sledgehammer\quoted_human_implode(' and ', array_keys($this->channels)), 'Available packages: ' . \Sledgehammer\quoted_human_implode(' and ', array_keys($this->packages)));
         }
         $packageLocation =& $this->channels[$this->packages[$package]]['packages'][$package];
     }
     $release = $this->findRelease($packageLocation, $version);
     if (\Sledgehammer\array_value($packageLocation, 'installed') === $version) {
         return;
     }
     $this->trigger('installing', $this, $package, $version);
     $tmpFolder = \Sledgehammer\TMP_DIR . 'PearInstaller/';
     $folderName = $package . '-' . $version;
     $tarFile = $tmpFolder . $folderName . '/package.tar';
     \Sledgehammer\mkdirs(dirname($tarFile));
     if (file_exists($tarFile) === false) {
         // Is this package already in the tmp folder
         Curl::download($release->g . '.tar', $tarFile);
     }
     chdir(dirname($tarFile));
     system('tar xf ' . escapeshellarg($tarFile), $exit);
     if ($exit !== 0) {
         throw new Exception('Unable to untar "' . $tarFile . '"');
     }
     if (file_exists(dirname($tarFile) . '/package2.xml')) {
         $info = simplexml_load_file(dirname($tarFile) . '/package2.xml');
     } else {
         $info = simplexml_load_file(dirname($tarFile) . '/package.xml');
     }
     // Install dependencies first
     foreach ($info->dependencies->required->package as $dependancy) {
         if ($dependancy->conflicts) {
             //				\Sledgehammer\notice('Dependancy "'.$dependancy->name.'" for "'.$package.'" <conflicts />');
             continue;
         }
         $this->install((string) $dependancy->name, array('channel' => (string) $dependancy->channel));
     }
     $renames = [];
     foreach ($info->phprelease as $release) {
         if ($release->count() > 0) {
             foreach ($release->filelist->install as $move) {
                 $renames[(string) $move['name']] = (string) $move['as'];
             }
         }
     }
     $files = $this->extractFiles($info->contents->dir, '', '/', $renames);
     foreach ($files as $file) {
         if (isset($this->targets[$file['role']])) {
             $dir = $this->targets[$file['role']];
             if (in_array($file['role'], array('doc', 'www'))) {
                 if (\Sledgehammer\text($file['to'])->startsWith($package) == false) {
                     $dir = $this->makePath($dir, $package);
                 }
             }
             $target = $this->makePath($dir, $file['to']);
             if (\Sledgehammer\mkdirs(dirname($target)) == false || is_writable(dirname($target)) == false) {
                 throw new Exception('Target "' . $target . '" is not writable');
             }
             $source = $this->makePath($tmpFolder . $folderName . '/' . $folderName, $file['from']);
             if (isset($file['tasks'])) {
                 $contents = file_get_contents($source);
                 foreach ($file['tasks'] as $task) {
                     $value = null;
                     if ($task['type'] === 'package-info') {
                         if ($task['to'] == 'version') {
                             $value = $version;
                         } elseif ($task['to'] == 'state') {
                             $value = (string) $info->stability->release;
                         }
                     } elseif ($task['type'] == 'pear-config') {
                         if (substr($task['to'], -4) === '_dir') {
                             $role = substr($task['to'], 0, -4);
                             if (isset($this->targets[$role])) {
                                 $value = $this->targets[$role];
                                 // @todo calculate relative paths
                                 \Sledgehammer\notice('Harcoding path "' . $value . '" into "' . $file['to'] . '"', $file);
                             }
                         } elseif ($task['to'] == 'php_bin') {
                             $value = trim(`which php`);
                             \Sledgehammer\notice('Harcoding path "' . $value . '" into "' . $file['to'] . '"', $file);
                         }
                     }
                     if ($task['task'] === 'replace') {
                         if ($value != '') {
                             $contents = str_replace($task['from'], $value, $contents);
                         } else {
                             \Sledgehammer\notice($task['type'] . ' "' . $task['to'] . '" not yet supported');
                         }
                     } else {
                         \Sledgehammer\notice('task "' . $task['task'] . '" not implemented');
                     }
                 }
                 file_put_contents($target, $contents);
             } else {
                 copy($source, $target);
             }
         }
     }
     \Sledgehammer\rmdir_recursive($tmpFolder . $folderName . '/' . $folderName);
     $packageLocation['installed'] = $version;
     $this->trigger('installed', $this, $package, $version);
 }