00001 <?php
00002 
00003 require_once "Varien/Pear.php";
00004 
00005 require_once "PEAR/PackageFileManager.php";
00006 require_once "PEAR/PackageFile/v1.php";
00007 require_once "PEAR/PackageFile/Generator/v1.php";
00008 
00009 require_once "PEAR/PackageFileManager2.php";
00010 require_once "PEAR/PackageFile/v2.php";
00011 require_once "PEAR/PackageFile/v2/rw.php";
00012 require_once "PEAR/PackageFile/v2/Validator.php";
00013 require_once "PEAR/PackageFile/Generator/v2.php";
00014 
00015 
00016 define ('PEAR_PACKAGEFILEMANAGER_NOSVNENTRIES', 1001);
00017 $GLOBALS['_PEAR_PACKAGEFILEMANAGER2_ERRORS']['en']['PEAR_PACKAGEFILEMANAGER_NOSVNENTRIES'] =
00018     'Directory "%s" is not a SVN directory (it must have the .svn/entries file)';
00019 
00020 class Varien_Pear_Package
00021 {
00022     protected $_data = array(
00023         'options' => array(
00024             'baseinstalldir'=>'',
00025             'filelistgenerator'=>'file',
00026             'packagedirectory'=>'.',
00027             'outputdirectory'=>'.',
00028         ),
00029         'package' => array(),
00030         'release' => array(),
00031     );
00032 
00033     protected $_pear;
00034     protected $_pfm;
00035 
00036     public function __construct()
00037     {
00038         $this->_pear = Varien_Pear::getInstance();
00039     }
00040 
00041     public function getPear()
00042     {
00043         return $this->_pear;
00044     }
00045 
00046     public function getPearConfig($key)
00047     {
00048         return $this->getPear()->getConfig()->get($key);
00049     }
00050 
00051     public function set($key, $data)
00052     {
00053         if (''===$key) {
00054             $this->_data = $data;
00055             return $this;
00056         }
00057 
00058         
00059         $keyArr = explode('/', $key);
00060 
00061         $ref = &$this->_data;
00062         for ($i=0, $l=sizeof($keyArr); $i<$l; $i++) {
00063             $k = $keyArr[$i];
00064             if (!isset($ref[$k])) {
00065                 $ref[$k] = array();
00066             }
00067             $ref = &$ref[$k];
00068         }
00069         $ref = $data;
00070 
00071         return $this;
00072     }
00073 
00074     public function get($key)
00075     {
00076         if (''===$key) {
00077             return $this->_data;
00078         }
00079 
00080         
00081         $keyArr = explode('/', $key);
00082         $data = $this->_data;
00083         foreach ($keyArr as $i=>$k) {
00084             if ($k==='') {
00085                 return null;
00086             }
00087             if (is_array($data)) {
00088                 if (!isset($data[$k])) {
00089                     return null;
00090                 }
00091                 $data = $data[$k];
00092             } else {
00093                 return null;
00094             }
00095         }
00096         return $data;
00097     }
00098 
00099     public function setPfm($pfm)
00100     {
00101         $this->_pfm = $pfm;
00102         return $this;
00103     }
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111     public function getPfm($package=null)
00112     {
00113         if (!$this->_pfm) {
00114             if (is_null($package)) {
00115                 $this->_pfm = new PEAR_PackageFileManager2;
00116                 $this->_pfm->setOptions($this->get('options'));
00117             } else {
00118                 $this->defineData();
00119 
00120                 $this->_pfm = PEAR_PackageFileManager2::importOptions($package, $this->get('options'));
00121                 if ($this->_pfm instanceof PEAR_Error) {
00122                     $e = PEAR_Exception('Could not instantiate PEAR_PackageFileManager2');
00123                     $e->errorObject = $this->_pfm;
00124                     throw $e;
00125                 }
00126             }
00127         }
00128         return $this->_pfm;
00129     }
00130 
00131     public function clearPackage()
00132     {
00133         $pfm = $this->getPfm();
00134         $pfm->clearContents();
00135         $pfm->clearCompatible();
00136         $pfm->clearDeps();
00137         $pfm->clearChangeLog();
00138         return $this;
00139     }
00140 
00141     public function generatePackage($make=false)
00142     {
00143         PEAR::setErrorHandling(PEAR_ERROR_DIE);
00144 
00145         $this->definePackage();
00146 
00147         $pfm = $this->getPfm();
00148         $pfm->addRelease();
00149 
00150         $this->defineRelease();
00151 
00152         $pfm->generateContents();
00153         $pfm1 = $pfm->exportCompatiblePackageFile1($this->get('options'));
00154 
00155         if ($make) {
00156             $pfm1->writePackageFile();
00157             $pfm->writePackageFile();
00158 
00159             $outputDir = $this->get('options/outputdirectory');
00160             MagePearWrapper::getInstance()->run('package', array(),
00161                 array($outputDir.'package.xml', $outputDir.'package2.xml')
00162             );
00163         } else {
00164             $pfm1->debugPackageFile();
00165             $pfm->debugPackageFile();
00166         }
00167 
00168         return $this;
00169     }
00170 
00171     public function defineData()
00172     {
00173         $this->set('options/outputdirectory', $this->getPear()->getPearDir().DS.'output');
00174         $this->set('options/filelistgenerator', 'php');
00175         $this->set('options/simpleoutput', true);
00176 
00177         return $this;
00178     }
00179 
00180     public function definePackage()
00181     {
00182         return $this;
00183     }
00184 
00185     public function defineRelease()
00186     {
00187         return $this;
00188     }
00189 }