Created
November 26, 2011 06:19
-
-
Save rsky/1395162 to your computer and use it in GitHub Desktop.
PEAR\PackageFileBuilder - Utility for PEAR_PackageFileManager2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The latest version can be found at https://github.com/rsky/PEAR_PackageFileBuilder | |
/** | |
* Utility for PEAR_PackageFileManager2. | |
* | |
* @package PEAR_PackageFileBuilder | |
* @author Ryusuke SEKIYAMA <rsky0711@gmail.com> | |
* @copyright Copyright (c) 2011 Ryusuke SEKIYAMA | |
* @license http://www.opensource.org/licenses/mit-license.php MIT License | |
*/ | |
namespace PEAR; | |
use PEAR_PackageFileManager2 as PackageFileManager; | |
class PackageFileBuilder | |
{ | |
/** | |
* @var PEAR_PackageFileManager2 | |
*/ | |
protected $_pfm; | |
/** | |
* Builds a package file manager. | |
* | |
* @param array $pkgInfo | |
* @param array $pfmOptions | |
* | |
* @return PEAR_PackageFileManager2 | |
*/ | |
public static function build(array $pkgInfo, | |
array $pfmOptions = array()) | |
{ | |
if (array_key_exists('packageFileManagerOptions', $pkgInfo)) { | |
$pfmOptions = array_merge($pkgInfo['packageFileManagerOptions'], | |
$pfmOptions); | |
} | |
$builder = new PackageFileBuilder($pfmOptions); | |
$builder->setPackage($pkgInfo['package']); | |
$builder->setState($pkgInfo['state']); | |
$builder->addMaintainers($pkgInfo['maintainers']); | |
if (array_key_exists('releases', $pkgInfo)) { | |
foreach ($pkgInfo['releases'] as $release) { | |
$builder->addRelease($release); | |
} | |
} else { | |
$builder->addRelease(); | |
} | |
if (array_key_exists('dependencies', $pkgInfo)) { | |
$builder->setDependencies($pkgInfo['dependencies']); | |
} | |
if (array_key_exists('tasks', $pkgInfo)) { | |
$builder->addTasks($pkgInfo['tasks']); | |
} | |
return $builder->getPakageFileManager(); | |
} | |
/** | |
* Constructor. | |
* | |
* @param array $options | |
*/ | |
public function __construct(array $options) | |
{ | |
$pfm = new PackageFileManager(); | |
$pfm->setOptions($options); | |
$this->_pfm = $pfm; | |
} | |
/** | |
* @return PEAR_PackageFileManager2 | |
*/ | |
public function getPakageFileManager() | |
{ | |
return $this->_pfm; | |
} | |
public function setPackage(array $package) | |
{ | |
$pfm = $this->_pfm; | |
$pfm->setPackageType($package['type']); | |
$pfm->setPackage($package['name']); | |
$pfm->setChannel($package['channel']); | |
$pfm->setSummary($package['summary']); | |
$pfm->setDescription($package['description']); | |
$this->setLicense($package['license']); | |
} | |
public function setLicense($license) | |
{ | |
if (is_array($license)) { | |
$this->_pfmCall('setLicense', | |
$license, | |
array('name'), | |
array('uri', 'filesource')); | |
} else { | |
$this->_pfm->setLicense($license); | |
} | |
} | |
public function setState($state) | |
{ | |
$pfm = $this->_pfm; | |
$pfm->setReleaseVersion($state['version']); | |
$pfm->setReleaseStability($state['stability']); | |
$pfm->setApiVersion($state['apiVersion']); | |
$pfm->setApiStability($state['apiStability']); | |
$pfm->setNotes($state['notes']); | |
} | |
public function addMaintainers(array $maintainers) | |
{ | |
$pfm = $this->_pfm;; | |
foreach ($maintainers as $maintainer) { | |
$role = $maintainer['role']; | |
$handle = $maintainer['handle']; | |
$name = $maintainer['name']; | |
$email = $maintainer['email']; | |
$active = 'yes'; | |
if (array_key_exists('active', $maintainer)) { | |
if (is_string($maintainer['active'])) { | |
$active = $maintainer['active']; | |
} elseif ($params['active']) { | |
$active = 'yes'; | |
} else { | |
$active = 'no'; | |
} | |
} | |
$pfm->addMaintainer($role, $handle, $name, $email, $active); | |
} | |
} | |
public function addRelease(array $release = array()) | |
{ | |
$pfm = $this->_pfm; | |
$pfm->addRelease(); | |
if (array_key_exists('osInstallCondition', $release)) { | |
$pfm->setOsInstallCondition($release['osInstallCondition']); | |
} | |
if (array_key_exists('installAs', $release)) { | |
foreach ($release['installAs'] as $path => $as) { | |
$pfm->addInstallAs($path, $as); | |
} | |
} | |
if (array_key_exists('ignoreToRelease', $release)) { | |
foreach ($release['ignoreToRelease'] as $path) { | |
$pfm->addIgnoreToRelease($path); | |
} | |
} | |
} | |
public function setDependencies(array $dependencies) | |
{ | |
if (array_key_exists('php', $dependencies)) { | |
$phpDep = $dependencies['php']; | |
$this->setPhpDependency($phpDep); | |
} | |
if (array_key_exists('pearinstaller', $dependencies)) { | |
$pearInstallerDep = $dependencies['pearinstaller']; | |
$this->setPearInstallerDependency($pearInstallerDep); | |
} | |
if (array_key_exists('packages', $dependencies)) { | |
$packageDeps = $dependencies['packages']; | |
$this->addPackageDependencies($packageDeps); | |
} | |
} | |
public function setPhpDependency($phpDep) | |
{ | |
if (is_array($phpDep)) { | |
$this->_pfmCall('setPhpDep', | |
$phpDep, | |
array('min'), | |
array('max', 'exclude')); | |
} else { | |
$this->_pfm->setPhpDep($phpDep); | |
} | |
} | |
public function setPearInstallerDependency($pearInstallerDep) | |
{ | |
if (is_array($pearInstallerDep)) { | |
$this->_pfmCall('setPearinstallerDep', | |
$pearInstallerDep, | |
array('min'), | |
array('max', 'recommended', 'exclude')); | |
} else { | |
$this->_pfm->setPearinstallerDep($pearInstallerDep); | |
} | |
} | |
public function addPackageDependencies(array $packageDeps) | |
{ | |
$requirements = array('type', 'name', 'channel'); | |
$optionals = array('min', 'max', 'recommended', 'exclude', | |
'providesextension', 'nodefault'); | |
foreach ($packageDeps as $packageDep) { | |
$this->_pfmCall('addPackageDepWithChannel', | |
$packageDep, | |
$requirements, | |
$optionals); | |
} | |
} | |
public function addTasks(array $tasks) | |
{ | |
$pfm = $this->_pfm; | |
if (array_key_exists('replacements', $tasks)) { | |
$this->addReplacements($tasks['replacements']); | |
} | |
if (array_key_exists('unixeol', $tasks)) { | |
foreach ($tasks['unixeol'] as $path) { | |
$pfm->addUnixEol($path); | |
} | |
} | |
if (array_key_exists('windowseol', $tasks)) { | |
foreach ($tasks['windowseol'] as $path) { | |
$pfm->addWindowsEol($path); | |
} | |
} | |
} | |
public function addReplacements(array $replacements) | |
{ | |
$pfm = $this->_pfm; | |
foreach ($replacements as $replacement) { | |
$type = $replacement['type']; | |
$from = $replacement['from']; | |
$to = $replacement['to']; | |
if (array_key_exists('path', $replacement)) { | |
$path = $replacement['path']; | |
$pfm->addReplacement($path, $type, $from, $to); | |
} else { | |
$pfm->addGlobalReplacement($type, $from, $to); | |
} | |
} | |
} | |
protected function _pfmCall($methodName, | |
array $parameters, | |
array $requirements, | |
array $optionals, | |
$default = false) | |
{ | |
$callable = array($this->_pfm, $methodName); | |
$arguments = array(); | |
foreach ($requirements as $key) { | |
$arguments[] = $parameters[$key]; | |
} | |
foreach ($optionals as $key) { | |
if (array_key_exists($key, $parameters)) { | |
$arguments[] = $parameters[$key]; | |
} else { | |
$arguments[] = $default; | |
} | |
} | |
return call_user_func_array($callable, $arguments); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment