From 827ab3aae8db1763e346ecded17a95eedb22f156 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Tue, 2 Nov 2010 05:18:31 +0100 Subject: [PATCH] Split the core of the current content handling into a tasks and filelist helper. --- .../lib/Components/Pear/Package/Contents.php | 166 +++++---------------- .../lib/Components/Pear/Package/Filelist.php | 36 +++++ .../Components/Pear/Package/Filelist/Default.php | 127 ++++++++++++++++ .../Components/Pear/Package/Filelist/Factory.php | 43 ++++++ components/lib/Components/Pear/Package/Tasks.php | 108 ++++++++++++++ 5 files changed, 348 insertions(+), 132 deletions(-) create mode 100644 components/lib/Components/Pear/Package/Filelist.php create mode 100644 components/lib/Components/Pear/Package/Filelist/Default.php create mode 100644 components/lib/Components/Pear/Package/Filelist/Factory.php create mode 100644 components/lib/Components/Pear/Package/Tasks.php diff --git a/components/lib/Components/Pear/Package/Contents.php b/components/lib/Components/Pear/Package/Contents.php index caa032a94..2bffb9622 100644 --- a/components/lib/Components/Pear/Package/Contents.php +++ b/components/lib/Components/Pear/Package/Contents.php @@ -35,6 +35,34 @@ class Components_Pear_Package_Contents private $_package; /** + * A tasks helper. + * + * @var Components_Pear_Package_Tasks + */ + private $_tasks; + + /** + * Provides access to the filelist handler. + * + * @var Components_Pear_Package_Filelist_Factory + */ + private $_filelist_factory; + + /** + * Constructor. + * + * @param Components_Pear_Package_Tasks $tasks A tasks helper. + * @param Components_Pear_Package_Filelist_Factory $factory Creates the filelist handler. + */ + public function __construct( + Components_Pear_Package_Tasks $tasks, + Components_Pear_Package_Filelist_Factory $factory + ) { + $this->_tasks = $tasks; + $this->_filelist_factory = $factory; + } + + /** * Set the package that should be handled. * * @param PEAR_PackageFileManager2 $package The package to work on. @@ -62,70 +90,13 @@ class Components_Pear_Package_Contents } /** - * Update the content listing of the provided package. + * Generate an updated contents listing. * * @return NULL */ - private function _updateContents() + private function _generateContents() { - $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); - } - } - } } /** @@ -135,79 +106,10 @@ class Components_Pear_Package_Contents */ public function update() { - $this->_updateContents(); + $taskfiles = $this->_tasks->denote($this->getPackage()); + $this->_generateContents(); + $this->_tasks->annotate($this->getPackage(), $taskfiles); - /** - * 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' - ); - } - } + $this->_filelist_factory->create($this->getPackage())->update(); } } \ No newline at end of file diff --git a/components/lib/Components/Pear/Package/Filelist.php b/components/lib/Components/Pear/Package/Filelist.php new file mode 100644 index 000000000..e50cff505 --- /dev/null +++ b/components/lib/Components/Pear/Package/Filelist.php @@ -0,0 +1,36 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ + +/** + * Components_Pear_Package_Filelist:: describes filelist handlers. + * + * 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 + */ +interface Components_Pear_Package_Filelist +{ + /** + * Update the file list. + * + * @return NULL + */ + public function update(); +} \ No newline at end of file diff --git a/components/lib/Components/Pear/Package/Filelist/Default.php b/components/lib/Components/Pear/Package/Filelist/Default.php new file mode 100644 index 000000000..6ab6c7474 --- /dev/null +++ b/components/lib/Components/Pear/Package/Filelist/Default.php @@ -0,0 +1,127 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ + +/** + * Components_Pear_Package_Filelist_Default:: is the default file list handler. + * + * 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_Filelist_Default +{ + /** + * The package to operate on. + * + * @var PEAR_PackageFile_v2_rw + */ + private $_package; + + /** + * Constructor. + * + * @param PEAR_PackageFile_v2_rw $package The package to operate on. + */ + public function __construct(PEAR_PackageFile_v2_rw $package) + { + $this->_package = $package; + } + + /** + * Update the file list. + * + * @return NULL + */ + public function update() + { + /** + * This is required to clear the + * section. + */ + $this->_package->setPackageType('php'); + + $contents = $this->_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': + $this->_package->addInstallAs( + $file['attribs']['name'], $components[1] + ); + break; + case 'js': + case 'horde': + $horde_role = true; + case 'locale': + $this->_package->addInstallAs( + $file['attribs']['name'], $file['attribs']['name'] + ); + break; + case 'migration': + $components = explode('/', $components[1]); + array_splice($components, count($components) - 1, 0, 'migration'); + $this->_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); + } + $this->_package->addInstallAs( + $file['attribs']['name'], $filename + ); + break; + } + } + + if ($horde_role) { + $roles = $this->_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) { + $this->_package->addUsesrole( + 'horde', 'Role', 'pear.horde.org' + ); + } + } + } +} \ No newline at end of file diff --git a/components/lib/Components/Pear/Package/Filelist/Factory.php b/components/lib/Components/Pear/Package/Filelist/Factory.php new file mode 100644 index 000000000..c2ef6437f --- /dev/null +++ b/components/lib/Components/Pear/Package/Filelist/Factory.php @@ -0,0 +1,43 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ + +/** + * Components_Pear_Package_Filelist_Factory:: handles the different file list + * generators. + * + * 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_Filelist_Factory +{ + /** + * Create the filelist handler. + * + * @param PEAR_PackageFile_v2_rw $package The package. + * + * @return Components_Pear_Package_Filelist The file list handler. + */ + public function create(PEAR_PackageFile_v2_rw $package) + { + return new Components_Pear_Package_Filelist_Default($package); + } +} \ No newline at end of file diff --git a/components/lib/Components/Pear/Package/Tasks.php b/components/lib/Components/Pear/Package/Tasks.php new file mode 100644 index 000000000..e8ef0df67 --- /dev/null +++ b/components/lib/Components/Pear/Package/Tasks.php @@ -0,0 +1,108 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Components + */ + +/** + * Components_Pear_Package_Tasks:: is a PEAR package oriented file task handler. + * + * 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_Tasks +{ + /** + * Return the files that have special tasks attached. + * + * @param PEAR_PackageFile_v2_rw $package The package. + * + * @return array The list of files with tasks attached. + */ + public function denote(PEAR_PackageFile_v2_rw $package) + { + $contents = $package->getContents(); + $taskfiles = array(); + foreach ($contents['dir']['file'] as $file) { + if (!isset($file['attribs'])) { + continue; + } + $atts = $file['attribs']; + unset($file['attribs']); + if (count($file)) { + $taskfiles[$atts['name']] = $file; + } + } + return $taskfiles; + } + + /** + * Return the files that have special tasks attached. + * + * @param PEAR_PackageFile_v2_rw $package The package. + * @param array $taskfiles The tasks to add. + * + * @return array The list of files with tasks attached. + */ + public function annotate(PEAR_PackageFile_v2_rw $package, array $taskfiles) + { + $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, + $package->_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'] + ); + $package->addTaskToFile($file['attribs']['name'], $task); + } + } + } + + } +} \ No newline at end of file -- 2.11.0