Extract the content updater into a separate class.
authorGunnar Wrobel <p@rdus.de>
Mon, 1 Nov 2010 16:06:13 +0000 (17:06 +0100)
committerGunnar Wrobel <p@rdus.de>
Mon, 1 Nov 2010 16:06:13 +0000 (17:06 +0100)
components/lib/Components/Pear/Factory.php
components/lib/Components/Pear/Package.php
components/lib/Components/Pear/Package/Contents.php [new file with mode: 0644]

index 6917925..6e13603 100644 (file)
@@ -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
index 4454f8d..de24a01 100644 (file)
@@ -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 <phprelease><filelist></filelist></phprelease>
-         * 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 (file)
index 0000000..caa032a
--- /dev/null
@@ -0,0 +1,213 @@
+<?php
+/**
+ * Components_Pear_Package_Contents:: handles the PEAR package content.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Components
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @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 <phprelease><filelist></filelist></phprelease>
+         * 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