| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Parser.php 1080 2007-02-10 18:17:08Z jwage $ |
| 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_Parser |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Parser |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | * @version $Revision: 1080 $ |
| 31 | * @author Jonathan H. Wage <jwage@mac.com> |
| 32 | */ |
| 33 | abstract class Doctrine_Parser |
| 34 | { |
| 35 | /** |
| 36 | * loadData |
| 37 | * |
| 38 | * Override in the parser driver |
| 39 | * |
| 40 | * @param string $array |
| 41 | * @return void |
| 42 | * @author Jonathan H. Wage |
| 43 | */ |
| 44 | abstract public function loadData($array); |
| 45 | |
| 46 | /** |
| 47 | * dumpData |
| 48 | * |
| 49 | * Override in the praser driver |
| 50 | * |
| 51 | * @param string $array |
| 52 | * @param string $path |
| 53 | * @return void |
| 54 | * @author Jonathan H. Wage |
| 55 | */ |
| 56 | abstract public function dumpData($array, $path = null); |
| 57 | |
| 58 | /** |
| 59 | * getParser |
| 60 | * |
| 61 | * Get instance of the specified parser |
| 62 | * |
| 63 | * @param string $type |
| 64 | * @return void |
| 65 | * @author Jonathan H. Wage |
| 66 | */ |
| 67 | static public function getParser($type) |
| 68 | { |
| 69 | $class = 'Doctrine_Parser_'.ucfirst($type); |
| 70 | |
| 71 | return new $class; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * load |
| 76 | * |
| 77 | * Interface for loading and parsing data from a file |
| 78 | * |
| 79 | * @param string $path |
| 80 | * @param string $type |
| 81 | * @return void |
| 82 | * @author Jonathan H. Wage |
| 83 | */ |
| 84 | static public function load($path, $type = 'xml') |
| 85 | { |
| 86 | $parser = self::getParser($type); |
| 87 | |
| 88 | return $parser->loadData($path); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * dump |
| 93 | * |
| 94 | * Interface for pulling and dumping data to a file |
| 95 | * |
| 96 | * @param string $array |
| 97 | * @param string $path |
| 98 | * @param string $type |
| 99 | * @return void |
| 100 | * @author Jonathan H. Wage |
| 101 | */ |
| 102 | static public function dump($array, $type = 'xml', $path = null) |
| 103 | { |
| 104 | $parser = self::getParser($type); |
| 105 | |
| 106 | return $parser->dumpData($array, $path); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * doLoad |
| 111 | * |
| 112 | * Get contents whether it is the path to a file file or a string of txt. |
| 113 | * Either should allow php code in it. |
| 114 | * |
| 115 | * @param string $path |
| 116 | * @return void |
| 117 | */ |
| 118 | public function doLoad($path) |
| 119 | { |
| 120 | ob_start(); |
| 121 | if ( ! file_exists($path)) { |
| 122 | $contents = $path; |
| 123 | $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dparser_' . microtime(); |
| 124 | |
| 125 | file_put_contents($path, $contents); |
| 126 | } |
| 127 | |
| 128 | include($path); |
| 129 | $contents = ob_get_clean(); |
| 130 | |
| 131 | return $contents; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * doDump |
| 136 | * |
| 137 | * @param string $data |
| 138 | * @param string $path |
| 139 | * @return void |
| 140 | */ |
| 141 | public function doDump($data, $path = null) |
| 142 | { |
| 143 | if ($path !== null) { |
| 144 | return file_put_contents($path, $data); |
| 145 | } else { |
| 146 | return $data; |
| 147 | } |
| 148 | } |
| 149 | } |