Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.doctrine-project.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.doctrine-project.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 parser driver
50
     *
51
     * @param string $array
52
     * @param string $path
53
     * @param string $charset The charset of the data being dumped
54
     * @return void
55
     * @author Jonathan H. Wage
56
     */
57
    abstract public function dumpData($array, $path = null, $charset = null);
58
 
59
    /**
60
     * getParser
61
     *
62
     * Get instance of the specified parser
63
     *
64
     * @param string $type
65
     * @return void
66
     * @author Jonathan H. Wage
67
     */
68
    static public function getParser($type)
69
    {
70
        $class = 'Doctrine_Parser_'.ucfirst($type);
71
 
72
        return new $class;
73
    }
74
 
75
    /**
76
     * load
77
     *
78
     * Interface for loading and parsing data from a file
79
     *
80
     * @param string $path
81
     * @param string $type
82
     * @return void
83
     * @author Jonathan H. Wage
84
     */
85
    static public function load($path, $type = 'xml')
86
    {
87
        $parser = self::getParser($type);
88
 
89
        return (array) $parser->loadData($path);
90
    }
91
 
92
    /**
93
     * dump
94
     *
95
     * Interface for pulling and dumping data to a file
96
     *
97
     * @param string $array
98
     * @param string $path
99
     * @param string $type
100
     * @param string $charset The charset of the data being dumped
101
     * @return void
102
     * @author Jonathan H. Wage
103
     */
104
    static public function dump($array, $type = 'xml', $path = null, $charset = null)
105
    {
106
        $parser = self::getParser($type);
107
 
108
        return $parser->dumpData($array, $path, $charset);
109
    }
110
 
111
    /**
112
     * doLoad
113
     *
114
     * Get contents whether it is the path to a file file or a string of txt.
115
     * Either should allow php code in it.
116
     *
117
     * @param string $path
118
     * @return void
119
     */
120
    public function doLoad($path)
121
    {
122
        ob_start();
123
        if ( ! file_exists($path)) {
124
            $contents = $path;
125
            $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dparser_' . microtime();
126
 
127
            file_put_contents($path, $contents);
128
        }
129
 
130
        include($path);
131
 
132
        // Fix #1569. Need to check if it's still all valid
133
        $contents = ob_get_clean(); //iconv("UTF-8", "UTF-8", ob_get_clean());
134
 
135
        return $contents;
136
    }
137
 
138
    /**
139
     * doDump
140
     *
141
     * @param string $data
142
     * @param string $path
143
     * @return void
144
     */
145
    public function doDump($data, $path = null)
146
    {
147
      if ($path !== null) {
148
            return file_put_contents($path, $data);
149
        } else {
150
            return $data;
151
        }
152
    }
153
}