| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Export.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $ |
| 4 | * |
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 16 | * |
| 17 | * This software consists of voluntary contributions made by many individuals |
| 18 | * and is licensed under the LGPL. For more information, see |
| 19 | * <http://www.phpdoctrine.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Data_Export |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Data |
| 27 | * @author Jonathan H. Wage <jwage@mac.com> |
| 28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 29 | * @link www.phpdoctrine.org |
| 30 | * @since 1.0 |
| 31 | * @version $Revision: 2552 $ |
| 32 | */ |
| 33 | class Doctrine_Data_Export extends Doctrine_Data |
| 34 | { |
| 35 | /** |
| 36 | * constructor |
| 37 | * |
| 38 | * @param string $directory |
| 39 | * @return void |
| 40 | */ |
| 41 | public function __construct($directory) |
| 42 | { |
| 43 | $this->setDirectory($directory); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * doExport |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function doExport() |
| 52 | { |
| 53 | $models = Doctrine::getLoadedModels(); |
| 54 | $specifiedModels = $this->getModels(); |
| 55 | |
| 56 | $data = array(); |
| 57 | |
| 58 | // for situation when the $models array is empty, but the $specifiedModels array isn't |
| 59 | if (empty($models)) { |
| 60 | $models = $specifiedModels; |
| 61 | } |
| 62 | |
| 63 | $models = Doctrine::initializeModels($models); |
| 64 | |
| 65 | foreach ($models AS $name) { |
| 66 | if ( ! empty($specifiedModels) AND ! in_array($name, $specifiedModels)) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | $results = Doctrine::getTable($name)->findAll(); |
| 71 | |
| 72 | if ($results->count() > 0) { |
| 73 | $data[$name] = $results; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | $data = $this->prepareData($data); |
| 78 | |
| 79 | return $this->dumpData($data); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * dumpData |
| 84 | * |
| 85 | * Dump the prepared data to the fixtures files |
| 86 | * |
| 87 | * @param string $array |
| 88 | * @return void |
| 89 | */ |
| 90 | public function dumpData(array $data) |
| 91 | { |
| 92 | $directory = $this->getDirectory(); |
| 93 | $format = $this->getFormat(); |
| 94 | |
| 95 | if ($this->exportIndividualFiles()) { |
| 96 | if (is_array($directory)) { |
| 97 | throw new Doctrine_Data_Exception('You must specify a single path to a folder in order to export individual files.'); |
| 98 | } else if ( ! is_dir($directory) && is_file($directory)) { |
| 99 | $directory = dirname($directory); |
| 100 | } |
| 101 | |
| 102 | foreach ($data as $className => $classData) { |
| 103 | if ( ! empty($classData)) { |
| 104 | Doctrine_Parser::dump(array($className => $classData), $format, $directory.DIRECTORY_SEPARATOR.$className.'.'.$format); |
| 105 | } |
| 106 | } |
| 107 | } else { |
| 108 | if (is_dir($directory)) { |
| 109 | $directory .= DIRECTORY_SEPARATOR . 'data.' . $format; |
| 110 | } |
| 111 | |
| 112 | if ( ! empty($data)) { |
| 113 | return Doctrine_Parser::dump($data, $format, $directory); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * prepareData |
| 120 | * |
| 121 | * Prepare the raw data to be exported with the parser |
| 122 | * |
| 123 | * @param string $data |
| 124 | * @return array |
| 125 | */ |
| 126 | public function prepareData($data) |
| 127 | { |
| 128 | $preparedData = array(); |
| 129 | |
| 130 | foreach ($data AS $className => $classData) { |
| 131 | foreach ($classData as $record) { |
| 132 | $className = get_class($record); |
| 133 | $recordKey = $className . '_' . implode('_', $record->identifier()); |
| 134 | |
| 135 | $recordData = $record->toArray(false); |
| 136 | |
| 137 | foreach ($recordData as $key => $value) { |
| 138 | if ( ! $value) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | // skip single primary keys, we need to maintain composite primary keys |
| 143 | $keys = $record->getTable()->getIdentifier(); |
| 144 | |
| 145 | if ( ! is_array($keys)) { |
| 146 | $keys = array($keys); |
| 147 | } |
| 148 | |
| 149 | if (count($keys) <= 1 && in_array($key, $keys)) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if ($relation = $this->isRelation($record, $key)) { |
| 154 | $relationAlias = $relation['alias']; |
| 155 | $relationRecord = $record->$relationAlias; |
| 156 | |
| 157 | // If collection then get first so we have an instance of the related record |
| 158 | if ($relationRecord instanceof Doctrine_Collection) { |
| 159 | $relationRecord = $relationRecord->getFirst(); |
| 160 | } |
| 161 | |
| 162 | // If relation is null or does not exist then continue |
| 163 | if ($relationRecord instanceof Doctrine_Null || !$relationRecord) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | // Get class name for relation |
| 168 | $relationClassName = get_class($relationRecord); |
| 169 | |
| 170 | $relationValue = $relationClassName . '_' . $value; |
| 171 | |
| 172 | $preparedData[$className][$recordKey][$relationAlias] = $relationValue; |
| 173 | } else { |
| 174 | $preparedData[$className][$recordKey][$key] = $value; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | $record->free(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return $preparedData; |
| 183 | } |
| 184 | } |