Split the core of the current content handling into a tasks and filelist helper.
authorGunnar Wrobel <p@rdus.de>
Tue, 2 Nov 2010 04:18:31 +0000 (05:18 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 2 Nov 2010 19:39:03 +0000 (20:39 +0100)
components/lib/Components/Pear/Package/Contents.php
components/lib/Components/Pear/Package/Filelist.php [new file with mode: 0644]
components/lib/Components/Pear/Package/Filelist/Default.php [new file with mode: 0644]
components/lib/Components/Pear/Package/Filelist/Factory.php [new file with mode: 0644]
components/lib/Components/Pear/Package/Tasks.php [new file with mode: 0644]

index caa032a..2bffb96 100644 (file)
@@ -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 <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'
-                );
-            }
-        }
+        $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 (file)
index 0000000..e50cff5
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Components_Pear_Package_Filelist:: describes filelist handlers.
+ *
+ * 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_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 <wrobel@pardus.de>
+ * @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 (file)
index 0000000..6ab6c74
--- /dev/null
@@ -0,0 +1,127 @@
+<?php
+/**
+ * Components_Pear_Package_Filelist_Default:: is the default file list handler.
+ *
+ * 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_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 <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_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 <phprelease><filelist></filelist></phprelease>
+         * 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 (file)
index 0000000..c2ef643
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Components_Pear_Package_Filelist_Factory:: handles the different file list
+ * generators.
+ *
+ * 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_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 <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_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 (file)
index 0000000..e8ef0df
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Components_Pear_Package_Tasks:: is a PEAR package oriented file task handler.
+ *
+ * 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_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 <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_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