Centralize creation of PEAR instances in the factory.
authorGunnar Wrobel <p@rdus.de>
Wed, 13 Oct 2010 07:02:54 +0000 (09:02 +0200)
committerGunnar Wrobel <p@rdus.de>
Wed, 13 Oct 2010 19:14:44 +0000 (21:14 +0200)
components/lib/Components/Pear/Factory.php
components/lib/Components/Pear/InstallLocation.php
components/lib/Components/Pear/Package.php
components/lib/Components/Runner/Installer.php

index 99a84ce..f6a0421 100644 (file)
@@ -72,6 +72,7 @@ class Components_Pear_Factory
     public function createPackageForInstallLocation($package_file, $config_file)
     {
         $package = $this->_dependencies->getInstance('Components_Pear_Package');
+        $package->setFactory($this);
         $package->setEnvironment($this->createInstallLocation($config_file));
         $package->setPackageXml($package_file);
         return $package;
@@ -87,9 +88,83 @@ class Components_Pear_Factory
     public function createPackageForDefaultLocation($package_file)
     {
         $package = $this->_dependencies->getInstance('Components_Pear_Package');
+        $package->setFactory($this);
         $package->setEnvironment($this->_dependencies->getInstance('Components_Pear_InstallLocation'));
         $package->setPackageXml($package_file);
         return $package;
     }
 
+    /**
+     * Return the PEAR Package representation.
+     *
+     * @param string                          $package_xml_path Path to the package.xml file.
+     * @param Components_Pear_InstallLocation $environment      The PEAR environment.
+     *
+     * @return PEAR_PackageFile
+     */
+    public function getPackageFile(
+        $package_xml_path,
+        Components_Pear_InstallLocation $environment
+    )
+    {
+        $pkg = new PEAR_PackageFile($environment->getPearConfig());
+        $package_file = $pkg->fromPackageFile($package_xml_path, PEAR_VALIDATE_NORMAL);
+        if ($package_file instanceOf PEAR_Error) {
+            throw new Components_Exception($package_file->getMessage());
+        }
+        return $package_file;
+    }
+
+    /**
+     * Return a writeable PEAR Package representation.
+     *
+     * @param string                          $package_xml_path Path to the package.xml file.
+     * @param Components_Pear_InstallLocation $environment      The PEAR environment.
+     *
+     * @return PEAR_PackageFileManager2
+     */
+    public function getPackageRwFile(
+        $package_xml_path,
+        Components_Pear_InstallLocation $environment
+    ) {
+        /**
+         * Ensure we setup the PEAR_Config according to the PEAR environment
+         * the user set.
+         */
+        $environment->getPearConfig();
+
+        if (!class_exists('PEAR_PackageFileManager2')) {
+            throw new Components_Exception(
+                'The Package "PEAR_PackageFileManager2" is missing in the PEAR environment. Please install it so that you can run this action.'
+            );
+        }
+
+        $package_rw_file = PEAR_PackageFileManager2::importOptions(
+            $package_xml_path,
+            array(
+                'packagedirectory' => dirname($package_xml_path),
+                'filelistgenerator' => 'file',
+                'clearcontents' => false,
+                'clearchangelog' => false,
+                'simpleoutput' => true,
+                'ignore' => array('*~', 'conf.php', 'CVS/*'),
+                'include' => '*',
+                'dir_roles' =>
+                array(
+                    'doc'       => 'doc',
+                    'example'   => 'doc',
+                    'js'        => 'horde',
+                    'lib'       => 'php',
+                    'migration' => 'data',
+                    'script'    => 'script',
+                    'test'      => 'test',
+                ),
+            )
+        );
+
+        if ($package_rw_file instanceOf PEAR_Error) {
+            throw new Components_Exception($package_file->getMessage());
+        }
+        return $package_rw_file;
+    }
 }
\ No newline at end of file
index d5ef104..81b4233 100644 (file)
@@ -136,6 +136,25 @@ class Components_Pear_InstallLocation
         }
     }
 
+    /**
+     * Set the paths to the resource directories.
+     *
+     * @param array $options The application options
+     *
+     * @return NULL
+     */
+    public function setResourceDirectories(array $options)
+    {
+        if (!empty($options['channelxmlpath'])) {
+            $this->setChannelDirectory($options['channelxmlpath']);
+        } else if (!empty($options['sourcepath'])) {
+            $this->setChannelDirectory($options['sourcepath']);
+        }
+        if (!empty($options['sourcepath'])) {
+            $this->setSourceDirectory($options['sourcepath']);
+        }
+    }
+
     public function createPearConfig()
     {
         if (empty($this->_config_file)) {
index fcf7bd0..44eaa0f 100644 (file)
@@ -42,6 +42,13 @@ class Components_Pear_Package
     private $_environment;
 
     /**
+     * The factory for PEAR class instances.
+     *
+     * @param Components_Pear_Factory
+     */
+    private $_factory;
+
+    /**
      * The path to the package XML file.
      *
      * @param string
@@ -85,6 +92,18 @@ class Components_Pear_Package
     }
 
     /**
+     * Define the factory that creates our PEAR dependencies.
+     *
+     * @param Components_Pear_Factory
+     *
+     * @return NULL
+     */
+    public function setFactory(Components_Pear_Factory $factory)
+    {
+        $this->_factory = $factory;
+    }
+
+    /**
      * Return the PEAR environment for this package.
      *
      * @return Components_Pear_InstallLocation
@@ -118,12 +137,10 @@ class Components_Pear_Package
     {
         $this->_checkSetup();
         if ($this->_package_file === null) {
-            $config = $this->getEnvironment()->getPearConfig();
-            $pkg = new PEAR_PackageFile($config);
-            $this->_package_file = $pkg->fromPackageFile($this->_package_xml_path, PEAR_VALIDATE_NORMAL);
-            if ($this->_package_file instanceOf PEAR_Error) {
-                throw new Components_Exception($this->_package_file->getMessage());
-            }
+            $this->_package_file = $this->_factory->getPackageFile(
+                $this->_package_xml_path,
+                $this->getEnvironment()
+            );
         }
         return $this->_package_file;
     }
@@ -137,44 +154,10 @@ class Components_Pear_Package
     {
         $this->_checkSetup();
         if ($this->_package_rw_file === null) {
-            /**
-             * Ensure we setup the PEAR_Config according to the PEAR environment
-             * the user set.
-             */
-            $this->getEnvironment()->getPearConfig();
-
-            if (!class_exists('PEAR_PackageFileManager2')) {
-                throw new Components_Exception(
-                    'The Package "PEAR_PackageFileManager2" is missing in the PEAR environment. Please install it so that you can run this action.'
-                );
-            }
-
-            $this->_package_rw_file = PEAR_PackageFileManager2::importOptions(
+            $this->_package_rw_file = $this->_factory->getPackageRwFile(
                 $this->_package_xml_path,
-                array(
-                    'packagedirectory' => dirname($this->_package_xml_path),
-                    'filelistgenerator' => 'file',
-                    'clearcontents' => false,
-                    'clearchangelog' => false,
-                    'simpleoutput' => true,
-                    'ignore' => array('*~', 'conf.php', 'CVS/*'),
-                    'include' => '*',
-                    'dir_roles' =>
-                    array(
-                        'doc'       => 'doc',
-                        'example'   => 'doc',
-                        'js'        => 'horde',
-                        'lib'       => 'php',
-                        'migration' => 'data',
-                        'script'    => 'script',
-                        'test'      => 'test',
-                    ),
-                )
+                $this->getEnvironment()
             );
-
-            if ($this->_package_rw_file instanceOf PEAR_Error) {
-                throw new Components_Exception($this->_package_file->getMessage());
-            }
         }
         return $this->_package_rw_file;
     }
@@ -188,8 +171,10 @@ class Components_Pear_Package
      */
     private function _checkSetup()
     {
-        if ($this->_environment === null || $this->_package_xml_path === null) {
-            throw new Components_Exception('You need to set the environment and the path to the package file first!');
+        if ($this->_environment === null
+            || $this->_package_xml_path === null
+            || $this->_factory === null) {
+            throw new Components_Exception('You need to set the factory, the environment and the path to the package file first!');
         }
     }
 
index 0c187e7..04b3262 100644 (file)
@@ -39,26 +39,26 @@ class Components_Runner_Installer
     private $_config;
 
     /**
-     * The install location.
+     * The factory for PEAR handlers.
      *
-     * @var Components_Pear_InstallLocation
+     * @var Components_Factory
      */
-    private $_location;
+    private $_factory;
 
     /**
      * Constructor.
      *
-     * @param Components_Config $config The configuration for the current job.
-     * @param Components_Pear_InstallLocation $location Represents the install
-     *                                                  location and its
-     *                                                  corresponding configuration.
+     * @param Components_Config       $config  The configuration for the current
+     *                                         job.
+     * @param Components_Pear_Factory $factory Generator for all required PEAR
+     *                                         components.
      */
     public function __construct(
         Components_Config $config,
-        Components_Pear_InstallLocation $location
+        Components_Pear_Factory $factory
     ) {
         $this->_config = $config;
-        $this->_location = $location;
+        $this->_factory = $factory;
     }
 
     public function run()
@@ -68,16 +68,10 @@ class Components_Runner_Installer
         if (!$location) {
             $location = $options['install'];
         }
-        $this->_location->setLocation($location, '.pearrc');
-        $pear_config = $this->_location->getPearConfig();
-        if (!empty($options['channelxmlpath'])) {
-            $this->_location->setChannelDirectory($options['channelxmlpath']);
-        } else if (!empty($options['sourcepath'])) {
-            $this->_location->setChannelDirectory($options['sourcepath']);
-        }
-        if (!empty($options['sourcepath'])) {
-            $this->_location->setSourceDirectory($options['sourcepath']);
-        }
+        $environment = $this->_factory
+            ->createInstallLocation($location . DIRECTORY_SEPARATOR . '.pearrc');
+        $environment->setResourceDirectories($options);
+        $pear_config = $environment->getPearConfig();
 
         $arguments = $this->_config->getArguments();
         $element = basename(realpath($arguments[0]));
@@ -86,6 +80,7 @@ class Components_Runner_Installer
         $this->_run = array();
 
         $this->_installHordeDependency(
+            $environment,
             $root_path,
             $element
         );
@@ -97,7 +92,7 @@ class Components_Runner_Installer
      * @param string $root_path   Root path to the Horde framework.
      * @param string $dependency  Package name of the dependency.
      */
-    private function _installHordeDependency($root_path, $dependency)
+    private function _installHordeDependency($environment, $root_path, $dependency)
     {
         $package_file = $root_path . DIRECTORY_SEPARATOR
             . $dependency . DIRECTORY_SEPARATOR . 'package.xml';
@@ -107,37 +102,37 @@ class Components_Runner_Installer
         }
 
         $parser = new PEAR_PackageFile_Parser_v2();
-        $parser->setConfig($this->_location->getPearConfig());
+        $parser->setConfig($environment->getPearConfig());
         $pkg = $parser->parse(file_get_contents($package_file), $package_file);
 
         $dependencies = $pkg->getDeps();
         foreach ($dependencies as $dependency) {
             if (isset($dependency['channel']) && $dependency['channel'] != 'pear.horde.org') {
-                $this->_location->provideChannel($dependency['channel']);
+                $environment->provideChannel($dependency['channel']);
                 $key = $dependency['channel'] . '/' . $dependency['name'];
                 if (in_array($key, $this->_run)) {
                     continue;
                 }
-                $this->_location->addPackageFromPackage(
+                $environment->addPackageFromPackage(
                     $dependency['channel'], $dependency['name']
                 );
                 $this->_run[] = $key;
             } else if (isset($dependency['channel'])) {
-                $this->_location->provideChannel($dependency['channel']);
+                $environment->provideChannel($dependency['channel']);
                 $key = $dependency['channel'] . '/' . $dependency['name'];
                 if (in_array($key, $this->_run)) {
                     continue;
                 }
                 $this->_run[] = $key;
-                $this->_installHordeDependency($root_path, $dependency['name']);
+                $this->_installHordeDependency($environment, $root_path, $dependency['name']);
             }
         }
         if (in_array($package_file, $this->_run)) {
             return;
         }
 
-        $this->_location->provideChannel($pkg->getChannel());
-        $this->_location->addPackageFromSource(
+        $environment->provideChannel($pkg->getChannel());
+        $environment->addPackageFromSource(
             $package_file
         );
         $this->_run[] = $package_file;