From 708c1b39aa026fc6f0f451dd62d280193b7c104e Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Mon, 1 Nov 2010 17:06:13 +0100 Subject: [PATCH] Extract the content updater into a separate class. --- components/lib/Components/Pear/Factory.php | 14 ++ components/lib/Components/Pear/Package.php | 147 +------------- .../lib/Components/Pear/Package/Contents.php | 213 +++++++++++++++++++++ 3 files changed, 229 insertions(+), 145 deletions(-) create mode 100644 components/lib/Components/Pear/Package/Contents.php diff --git a/components/lib/Components/Pear/Factory.php b/components/lib/Components/Pear/Factory.php index 691792594..6e136032e 100644 --- a/components/lib/Components/Pear/Factory.php +++ b/components/lib/Components/Pear/Factory.php @@ -334,4 +334,18 @@ class Components_Pear_Factory $dependencies->setPackage($package); return $dependencies; } + + /** + * Create a package content helper. + * + * @param PEAR_PackageFileManager2 $package The package. + * + * @return Components_Pear_Package_Contents The contents helper. + */ + public function createContents(PEAR_PackageFileManager2 $package) + { + $contents = $this->_dependencies->createInstance('Components_Pear_Package_Contents'); + $contents->setPackage($package); + return $contents; + } } \ No newline at end of file diff --git a/components/lib/Components/Pear/Package.php b/components/lib/Components/Pear/Package.php index 4454f8db9..de24a01de 100644 --- a/components/lib/Components/Pear/Package.php +++ b/components/lib/Components/Pear/Package.php @@ -289,75 +289,6 @@ class Components_Pear_Package } /** - * Update the content listing of the provided package. - * - * @param PEAR_PackageFileManager2 $package The package to update. - * - * @return NULL - */ - private function _updateContents(PEAR_PackageFileManager2 $package) - { - $contents = $package->getContents(); - $contents = $contents['dir']['file']; - $taskfiles = array(); - foreach ($contents as $file) { - if (!isset($file['attribs'])) { - continue; - } - $atts = $file['attribs']; - unset($file['attribs']); - if (count($file)) { - $taskfiles[$atts['name']] = $file; - } - } - - $package->generateContents(); - - $updated = $package->getContents(); - $updated = $updated['dir']['file']; - foreach ($updated as $file) { - if (!isset($file['attribs'])) { - continue; - } - if (isset($taskfiles[$file['attribs']['name']])) { - foreach ($taskfiles[$file['attribs']['name']] as $tag => $raw) { - $taskname = $package->getTask($tag) . '_rw'; - if (!class_exists($taskname)) { - throw new Components_Exception( - sprintf('Read/write task %s is missing!', $taskname) - ); - } - $logger = new stdClass; - $task = new $taskname( - $package, - $this->getEnvironment()->getPearConfig(), - $logger, - '' - ); - switch ($taskname) { - case 'PEAR_Task_Replace_rw': - $task->setInfo( - $raw['attribs']['from'], - $raw['attribs']['to'], - $raw['attribs']['type'] - ); - break; - default: - throw new Components_Exception( - sprintf('Unsupported task type %s!', $tag) - ); - } - $task->init( - $raw, - $file['attribs'] - ); - $package->addTaskToFile($file['attribs']['name'], $task); - } - } - } - } - - /** * Return an updated package description. * * @return PEAR_PackageFileManager2 The updated package. @@ -365,82 +296,8 @@ class Components_Pear_Package private function _getUpdatedPackageFile() { $package = $this->_getPackageRwFile(); - - $this->_updateContents($package); - - /** - * This is required to clear the - * section. - */ - $package->setPackageType('php'); - - $contents = $package->getContents(); - $files = $contents['dir']['file']; - $horde_role = false; - - foreach ($files as $file) { - if (!isset($file['attribs'])) { - continue; - } - $components = explode('/', $file['attribs']['name'], 2); - switch ($components[0]) { - case 'doc': - case 'example': - case 'lib': - case 'test': - case 'data': - $package->addInstallAs( - $file['attribs']['name'], $components[1] - ); - break; - case 'js': - case 'horde': - $horde_role = true; - case 'locale': - $package->addInstallAs( - $file['attribs']['name'], $file['attribs']['name'] - ); - break; - case 'migration': - $components = explode('/', $components[1]); - array_splice($components, count($components) - 1, 0, 'migration'); - $package->addInstallAs( - $file['attribs']['name'], implode('/', $components) - ); - break; - case 'bin': - case 'script': - $filename = basename($file['attribs']['name']); - if (substr($filename, strlen($filename) - 4) == '.php') { - $filename = substr($filename, 0, strlen($filename) - 4); - } - $package->addInstallAs( - $file['attribs']['name'], $filename - ); - break; - } - } - - if ($horde_role) { - $roles = $package->getUsesrole(); - if (!empty($roles)) { - if (isset($roles['role'])) { - $roles = array($roles); - } - foreach ($roles as $role) { - if (isset($role['role']) && $role['role'] == 'horde') { - $horde_role = false; - break; - } - } - } - if ($horde_role) { - $package->addUsesrole( - 'horde', 'Role', 'pear.horde.org' - ); - } - } - + $contents = $this->_factory->createContents($package); + $contents->update(); return $package; } diff --git a/components/lib/Components/Pear/Package/Contents.php b/components/lib/Components/Pear/Package/Contents.php new file mode 100644 index 000000000..caa032a94 --- /dev/null +++ b/components/lib/Components/Pear/Package/Contents.php @@ -0,0 +1,213 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ + +/** + * Components_Pear_Package_Contents:: handles the PEAR package content. + * + * Copyright 2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @category Horde + * @package Components + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ +class Components_Pear_Package_Contents +{ + /** + * The package to work on. + * + * @var PEAR_PackageFileManager2 + */ + private $_package; + + /** + * Set the package that should be handled. + * + * @param PEAR_PackageFileManager2 $package The package to work on. + * + * @return NULL + */ + public function setPackage(PEAR_PackageFileManager2 $package) + { + $this->_package = $package; + } + + /** + * Set the package that should be handled. + * + * @param PEAR_PackageFileManager2 $package The package to work on. + * + * @return NULL + */ + public function getPackage() + { + if (empty($this->_package)) { + throw new Components_Exception('Set the package first!'); + } + return $this->_package; + } + + /** + * Update the content listing of the provided package. + * + * @return NULL + */ + private function _updateContents() + { + $contents = $this->getPackage()->getContents(); + $contents = $contents['dir']['file']; + $taskfiles = array(); + foreach ($contents as $file) { + if (!isset($file['attribs'])) { + continue; + } + $atts = $file['attribs']; + unset($file['attribs']); + if (count($file)) { + $taskfiles[$atts['name']] = $file; + } + } + + $this->getPackage()->generateContents(); + + $updated = $this->getPackage()->getContents(); + $updated = $updated['dir']['file']; + foreach ($updated as $file) { + if (!isset($file['attribs'])) { + continue; + } + if (isset($taskfiles[$file['attribs']['name']])) { + foreach ($taskfiles[$file['attribs']['name']] as $tag => $raw) { + $taskname = $this->getPackage()->getTask($tag) . '_rw'; + if (!class_exists($taskname)) { + throw new Components_Exception( + sprintf('Read/write task %s is missing!', $taskname) + ); + } + $logger = new stdClass; + $task = new $taskname( + $this->getPackage(), + $this->getPackage()->_config, + $logger, + '' + ); + switch ($taskname) { + case 'PEAR_Task_Replace_rw': + $task->setInfo( + $raw['attribs']['from'], + $raw['attribs']['to'], + $raw['attribs']['type'] + ); + break; + default: + throw new Components_Exception( + sprintf('Unsupported task type %s!', $tag) + ); + } + $task->init( + $raw, + $file['attribs'] + ); + $this->getPackage()->addTaskToFile($file['attribs']['name'], $task); + } + } + } + } + + /** + * Return an updated package description. + * + * @return PEAR_PackageFileManager2 The updated package. + */ + public function update() + { + $this->_updateContents(); + + /** + * This is required to clear the + * section. + */ + $this->getPackage()->setPackageType('php'); + + $contents = $this->getPackage()->getContents(); + $files = $contents['dir']['file']; + $horde_role = false; + + foreach ($files as $file) { + if (!isset($file['attribs'])) { + continue; + } + $components = explode('/', $file['attribs']['name'], 2); + switch ($components[0]) { + case 'doc': + case 'example': + case 'lib': + case 'test': + case 'data': + $this->getPackage()->addInstallAs( + $file['attribs']['name'], $components[1] + ); + break; + case 'js': + case 'horde': + $horde_role = true; + case 'locale': + $this->getPackage()->addInstallAs( + $file['attribs']['name'], $file['attribs']['name'] + ); + break; + case 'migration': + $components = explode('/', $components[1]); + array_splice($components, count($components) - 1, 0, 'migration'); + $this->getPackage()->addInstallAs( + $file['attribs']['name'], implode('/', $components) + ); + break; + case 'bin': + case 'script': + $filename = basename($file['attribs']['name']); + if (substr($filename, strlen($filename) - 4) == '.php') { + $filename = substr($filename, 0, strlen($filename) - 4); + } + $this->getPackage()->addInstallAs( + $file['attribs']['name'], $filename + ); + break; + } + } + + if ($horde_role) { + $roles = $this->getPackage()->getUsesrole(); + if (!empty($roles)) { + if (isset($roles['role'])) { + $roles = array($roles); + } + foreach ($roles as $role) { + if (isset($role['role']) && $role['role'] == 'horde') { + $horde_role = false; + break; + } + } + } + if ($horde_role) { + $this->getPackage()->addUsesrole( + 'horde', 'Role', 'pear.horde.org' + ); + } + } + } +} \ No newline at end of file -- 2.11.0