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