File indexing completed on 2025-01-19 05:21:37

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @subpackage Framework
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Tool_Project_Profile_FileParser_Xml
0025  */
0026 // require_once 'Zend/Tool/Project/Profile/FileParser/Xml.php';
0027 
0028 /**
0029  * @see Zend_Tool_Project_Profile_Resource_Container
0030  */
0031 // require_once 'Zend/Tool/Project/Profile/Resource/Container.php';
0032 
0033 /**
0034  * This class is the front most class for utilizing Zend_Tool_Project
0035  *
0036  * A profile is a hierarchical set of resources that keep track of
0037  * items within a specific project.
0038  *
0039  * @category   Zend
0040  * @package    Zend_Tool
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_Tool_Project_Profile extends Zend_Tool_Project_Profile_Resource_Container
0045 {
0046 
0047     /**
0048      * @var bool
0049      */
0050     protected static $_traverseEnabled = false;
0051 
0052     /**
0053      * Constructor, standard usage would allow the setting of options
0054      *
0055      * @param array $options
0056      * @return bool
0057      */
0058     public function __construct($options = null)
0059     {
0060         if ($options) {
0061             $this->setOptions($options);
0062         }
0063 
0064         $this->_topResources = new Zend_Tool_Project_Profile_Resource_Container();
0065     }
0066 
0067     /**
0068      * Process options and either set a profile property or
0069      * set a profile 'attribute'
0070      *
0071      * @param array $options
0072      */
0073     public function setOptions(Array $options)
0074     {
0075         $this->setAttributes($options);
0076     }
0077 
0078     /**
0079      * getIterator() - reqruied by the RecursiveIterator interface
0080      *
0081      * @return RecursiveIteratorIterator
0082      */
0083     public function getIterator()
0084     {
0085         // require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php';
0086 
0087         return new RecursiveIteratorIterator(
0088             new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($this),
0089             RecursiveIteratorIterator::SELF_FIRST
0090             );
0091     }
0092 
0093     /**
0094      * loadFromData() - Load a profile from data provided by the
0095      * 'profilData' attribute
0096      *
0097      */
0098     public function loadFromData()
0099     {
0100         if (!isset($this->_attributes['profileData'])) {
0101             // require_once 'Zend/Tool/Project/Exception.php';
0102             throw new Zend_Tool_Project_Exception('loadFromData() must have "profileData" set.');
0103         }
0104 
0105         $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
0106         $profileFileParser->unserialize($this->_attributes['profileData'], $this);
0107 
0108         $this->rewind();
0109     }
0110 
0111     /**
0112      * isLoadableFromFile() - can a profile be loaded from a file
0113      *
0114      * wether or not a profile can be loaded from the
0115      * file in attribute 'projectProfileFile', or from a file named
0116      * '.zfproject.xml' inside a directory in key 'projectDirectory'
0117      *
0118      * @return bool
0119      */
0120     public function isLoadableFromFile()
0121     {
0122         if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
0123             return false;
0124         }
0125 
0126         if (isset($this->_attributes['projectProfileFile'])) {
0127             $projectProfileFilePath = $this->_attributes['projectProfileFile'];
0128             if (!file_exists($projectProfileFilePath)) {
0129                 return false;
0130             }
0131         } else {
0132             $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
0133             if (!file_exists($projectProfileFilePath)) {
0134                 return false;
0135             }
0136         }
0137 
0138         return true;
0139     }
0140 
0141     /**
0142      * loadFromFile() - Load data from file
0143      *
0144      * this attempts to load a project profile file from a variety of locations depending
0145      * on what information the user provided vie $options or attributes, specifically the
0146      * 'projectDirectory' or 'projectProfileFile'
0147      *
0148      */
0149     public function loadFromFile()
0150     {
0151         // if no data is supplied, need either a projectProfileFile or a projectDirectory
0152         if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
0153             // require_once 'Zend/Tool/Project/Exception.php';
0154             throw new Zend_Tool_Project_Exception('loadFromFile() must have at least "projectProfileFile" or "projectDirectory" set.');
0155         }
0156 
0157         if (isset($this->_attributes['projectProfileFile'])) {
0158             $projectProfileFilePath = $this->_attributes['projectProfileFile'];
0159             if (!file_exists($projectProfileFilePath)) {
0160                 // require_once 'Zend/Tool/Project/Exception.php';
0161                 throw new Zend_Tool_Project_Exception('"projectProfileFile" was supplied but file was not found at location ' . $projectProfileFilePath);
0162             }
0163             $this->_attributes['projectDirectory'] = dirname($projectProfileFilePath);
0164         } else {
0165             $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
0166             if (!file_exists($projectProfileFilePath)) {
0167                 // require_once 'Zend/Tool/Project/Exception.php';
0168                 throw new Zend_Tool_Project_Exception('"projectDirectory" was supplied but no profile file file was not found at location ' . $projectProfileFilePath);
0169             }
0170             $this->_attributes['projectProfileFile'] = $projectProfileFilePath;
0171         }
0172 
0173         $profileData = file_get_contents($projectProfileFilePath);
0174 
0175         $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
0176         $profileFileParser->unserialize($profileData, $this);
0177 
0178         $this->rewind();
0179     }
0180 
0181     /**
0182      * storeToFile() - store the current profile to file
0183      *
0184      * This will store the profile in memory to a place on disk determined by the attributes
0185      * available, specifically if the key 'projectProfileFile' is available
0186      *
0187      */
0188     public function storeToFile()
0189     {
0190         $file = null;
0191 
0192         if (isset($this->_attributes['projectProfileFile'])) {
0193             $file = $this->_attributes['projectProfileFile'];
0194         }
0195 
0196         if ($file == null) {
0197             // require_once 'Zend/Tool/Project/Exception.php';
0198             throw new Zend_Tool_Project_Exception('storeToFile() must have a "projectProfileFile" attribute set.');
0199         }
0200 
0201         $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
0202         $xml = $parser->serialize($this);
0203         file_put_contents($file, $xml);
0204     }
0205 
0206     /**
0207      * storeToData() - create a string representation of the profile in memory
0208      *
0209      * @return string
0210      */
0211     public function storeToData()
0212     {
0213         $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
0214         $xml = $parser->serialize($this);
0215         return $xml;
0216     }
0217 
0218     /**
0219      * __toString() - cast this profile to string to be able to view it.
0220      *
0221      * @return string
0222      */
0223     public function __toString()
0224     {
0225         $string = '';
0226         foreach ($this as $resource) {
0227             $string .= $resource->getName() . PHP_EOL;
0228             $rii = new RecursiveIteratorIterator($resource, RecursiveIteratorIterator::SELF_FIRST);
0229             foreach ($rii as $item) {
0230                 $string .= str_repeat('  ', $rii->getDepth()+1) . $item->getName()
0231                         . ((count($attributes = $item->getAttributes()) > 0) ? ' [' . http_build_query($attributes) . ']' : '')
0232                         . PHP_EOL;
0233             }
0234         }
0235         return $string;
0236     }
0237 }