Source for file PearWrapper.php
Documentation is available at PearWrapper.php
// vim: foldmethod=marker
* @author ICHII Takashi <ichii386@schweetheart.jp>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @version $Id: 0d5ac26a60682608f56c627841bb2f745d99a3c1 $
require_once 'PEAR/Config.php';
require_once 'PEAR/Command.php';
require_once 'PEAR/PackageFile.php';
* wrapper class for PEAR_Command
* This class should be instantiated in ethna handler.
* @author ICHII Takashi <ichii386@schweetheart.jp>
/** @protected string channel url of ethna repositry */
/** @protected string target, 'master' or 'local' */
/** @protected object controller object collesponding to the target */
/** @protected object PEAR_Config PEAR_Config object */
/** @protected object PEAR_Registry PEAR_Registry object */
/** @protected object PEAR_Frontend PEAR_Frontend(_CLI) object */
/** @protected array options for pearcmd */
// {{{ constructor, initializer
* Ethna_PearWrapper constructor
* setup PEAR_Config and so on.
* @param string $target whether 'master' or 'local'
* @param string|null$app_dir local application directory.
* @param string|null$channel channel for the package repository.
* @return true|Ethna_Error
public function init($target, $app_dir =
null, $channel =
null)
if ($target ==
'master') {
// default target is 'local'.
PEAR_Command::setFrontendType('CLI');
$this->ui =
PEAR_Command::getFrontendObject();
// set PEAR's error handling
// TODO: if PEAR/Command/Install.php is newer than 1.117, displayError goes well.
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this->ui, 'displayFatalError'));
} else if (isset
($master_setting["channel_{$target}"])) {
$this->channel =
$master_setting["channel_{$target}"];
if ($target ==
'master') {
if ($target ==
'master') {
$ret =
$this->_setMasterConfig();
* @return true|Ethna_Error
private function _setMasterConfig()
$this->config =
PEAR_Config::singleton();
$reg =
$this->config->getRegistry();
if ($reg->channelExists($this->channel) ==
false) {
* @return true|Ethna_Error
'php_dir' =>
"{
$base}/skel
",
'cache_dir' =>
"{
$tmp}/.pear/cache
",
'download_dir' =>
"{
$tmp}/.pear/download
",
'temp_dir' =>
"{
$tmp}/.pear/temp
",
'doc_dir' =>
"{
$tmp}/.pear/doc
",
'ext_dir' =>
"{
$tmp}/.pear/ext
",
'data_dir' =>
"{
$tmp}/.pear/data
",
'test_dir' =>
"{
$tmp}/.pear/test
",
foreach ($dirs as $key =>
$dir) {
$pearrc =
"{
$base}/skel/.pearrc
";
$this->config =
PEAR_Config::singleton($pearrc);
// read local .pearrc if exists.
$this->config->readConfigFile($pearrc);
foreach ($dirs as $key =>
$dir) {
$this->config->set($key, $dir);
$reg =
$this->config->getRegistry();
if ($reg->channelExists($this->channel) ==
false) {
$this->config->writeConfigFile();
* @return true|Ethna_Error
$r =
$this->_run('clear-cache', array(), array());
* @return true|Ethna_Error
$r =
$this->_run('channel-discover', array(), array($this->channel));
* whether channel discovered or not
* @return true|Ethna_Error
$r =
$this->_run('channel-update', array(), array($this->channel));
// {{{ _doInstallOrUpgrade
* @param string $command 'install' or 'upgrade'
* @param string $package package string
* @return true|Ethna_Error
private function _doInstallOrUpgrade($command, $package)
$r =
$this->_run($command, array(), array($package));
* @param string $pkg_name package name.
* @param string $state package state.
* @return true|Ethna_Error
public function doInstall($pkg_name, $state =
null)
$pkg =
"{
$this->channel}/{
$pkg_name}";
$pkg = "{
$pkg}-{
$state}";
$r = $this->_doInstallOrUpgrade('install', $pkg);
* do install from local tgz file
* @param string $pkg_file package filename
* @return true|Ethna_Error
public function doInstallFromTgz($pkg_file)
$r = $this->_doInstallOrUpgrade('install', $pkg_file);
* @param string $pkg_name package name.
* @param string $state package state.
* @return true|Ethna_Error
public function doUpgrade($pkg_name, $state = null)
$pkg = "{
$this->channel}/{
$pkg_name}";
$pkg = "{
$pkg}-{
$state}";
$r = $this->_doInstallOrUpgrade('upgrade', $pkg);
* do upgrade from local tgz file
* @param string $pkg_file package filename
* @return true|Ethna_Error
public function doUpgradeFromTgz($pkg_file)
$r = $this->_doInstallOrUpgrade('upgrade', $pkg_file);
* check package installed
* @param string $package package name
public function isInstalled($package)
* @param string $package package name
* @return string version string
public function getVersion($package)
return $pobj->getVersion();
* @param string $package package name
* @return string version string
public function getState($package)
return $pobj->getState();
* do uninstall (packages installed with ethna command)
* @return true|Ethna_Error
public function doUninstall($package)
return Ethna::raiseNotice("{
$this->channel}/{
$package} is not installed.
");
$r = $this->_run('uninstall', array(), array("{
$this->channel}/{
$package}"));
return Ethna::raiseNotice("uninstall failed: {
$this->channel}/{
$package}");
// {{{ getPackageNameFromTgz
* get package info from tar/tgz file.
* @param string $filename package file name.
* @return string package name
public function getPackageNameFromTgz($filename)
$config = PEAR_Config::singleton();
$packagefile = new PEAR_PackageFile($config);
$info = $packagefile->fromTgzFile($filename, PEAR_VALIDATE_NORMAL);
if (PEAR::isError($info)) {
$info_array = $info->toArray();
return $info_array['name'];
// {{{ getCanonicalPackageName
* get canonical package name (case sensitive)
* @param string $package package name.
* @return string canonical name
public function getCanonicalPackageName($package)
return Ethna::raiseNotice("{
$this->channel}/{
$package} is not installed.
");
$cname =
$pobj->getName();
// {{{ getInstalledPackageList
* get installed package list
* @return array installed package list
public function getInstalledPackageList()
* do info (packages installed with ethna command)
* @param string $package package name.
* @return true|Ethna_Error
public function doInfo($package)
return $this->_run('info', array(), array("{
$this->channel}/{
$package}"));
* do info (packages installable with ethna command)
* @param string $package package name.
* @return true|Ethna_Error
public function doRemoteInfo($package)
return $this->_run('remote-info', array(), array("{
$this->channel}/{
$package}"));
* @return true|Ethna_Error
public function doUpgradeAll()
return $this->_run('upgrade-all', array('channel' =>
"{
$this->channel}"), array());
* do list (packages installed with ethna command)
* @return true|Ethna_Error
return $this->_run('list', array('channel' =>
$this->channel), array());
* do remote-list (packages installable with ethna command)
* @return true|Ethna_Error
public function doRemoteList()
return $this->_run('remote-list', array('channel' =>
$this->channel), array());
* @param string $command command name
* @param array $options options
* @param array $params parameters
* @return true|Ethna_Error
* @see PEAR_Command_Common::run, etc.
protected function _run($command, $options, $params)
return Ethna::raiseError('configuration not initialized.');
$cmd = PEAR_Command::factory($command, $this->config);
if (PEAR::isError($cmd)) {
$pearopts = $this->_getPearOpt($cmd, $command, $this->_pearopt);
$options =
array_merge($pearopts, $options);
$ret = $cmd->run($command, $options, $params);
if (PEAR::isError($ret)) {
* provide yes-or-no dialog.
public function confirmDialog($message, $default = 'yes')
$ret = $this->ui->userConfirm($message);
* @param array $headline headline
* @param array $rows rows which have the same size as headline's.
public function displayTable($caption, $headline, $rows)
foreach (array_keys($headline) as $k) {
$headline[$k] = sprintf('% -8s', $headline[$k]);
$data = array('caption' => $caption,
$this->ui->outputData($data);
public function setPearOpt($pearopt)
private function _getPearOpt($cmd_obj, $cmd_str, $opt_array)
$short_args = $long_args = null;
PEAR_Command::getGetOptArgs($cmd_str, $short_args, $long_args);
$opt = new Ethna_Getopt();
$opt_arg = $opt->getopt($opt_array, $short_args, $long_args);
if (Ethna::isError($opt_arg)) return array();
foreach ($opt_arg[0] as $tmp) {
if ($val === null) $val = true;
$cmd_opts = $cmd_obj->getOptions($cmd_str);
foreach ($cmd_opts as $o =>
$d) {
if (isset($d['shortopt']) && $d['shortopt'] == $opt) {
if (substr($opt, 0, 2) == '--') $opts[substr($opt, 2)] = $val;
Documentation generated on Fri, 11 Nov 2011 03:58:53 +0900 by phpDocumentor 1.4.3