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: Lib.php 7490 2010-03-29 19:53:27Z 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_Lib has not commonly used static functions, mostly for debugging purposes
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Lib
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Lib
34
{
35
    /**
36
     * Generates a human readable representation of a record's state.
37
     *
38
     * This method translates a Doctrine_Record state (integer constant)
39
     * in an english string.
40
     * @see Doctrine_Record::STATE_* constants
41
     *
42
     * @param integer $state    the state of record
43
     * @return string           description of given state
44
     */
45
    public static function getRecordStateAsString($state)
46
    {
47
        switch ($state) {
48
            case Doctrine_Record::STATE_PROXY:
49
                return "proxy";
50
                break;
51
            case Doctrine_Record::STATE_CLEAN:
52
                return "persistent clean";
53
                break;
54
            case Doctrine_Record::STATE_DIRTY:
55
                return "persistent dirty";
56
                break;
57
            case Doctrine_Record::STATE_TDIRTY:
58
                return "transient dirty";
59
                break;
60
            case Doctrine_Record::STATE_TCLEAN:
61
                return "transient clean";
62
                break;
63
        }
64
    }
65
 
66
    /**
67
     * Dumps a record.
68
     *
69
     * This method returns an html representation of a given
70
     * record, containing keys, state and data.
71
     *
72
     * @param Doctrine_Record $record
73
     * @return string
74
     */
75
    public static function getRecordAsString(Doctrine_Record $record)
76
    {
77
        $r[] = '<pre>';
78
        $r[] = 'Component  : ' . $record->getTable()->getComponentName();
79
        $r[] = 'ID         : ' . Doctrine_Core::dump($record->identifier());
80
        $r[] = 'References : ' . count($record->getReferences());
81
        $r[] = 'State      : ' . Doctrine_Lib::getRecordStateAsString($record->state());
82
        $r[] = 'OID        : ' . $record->getOID();
83
        $r[] = 'data       : ' . Doctrine_Core::dump($record->getData(), false);
84
        $r[] = '</pre>';
85
 
86
        return implode("\n",$r)."<br />";
87
    }
88
 
89
    /**
90
     * Generates a human readable representation of a connection's state.
91
     *
92
     * This method translates a Doctrine_Connection state (integer constant)
93
     * in a english description.
94
     * @see Doctrine_Transaction::STATE_* constants
95
     * @param integer $state    state of the connection as a string
96
     * @return string
97
     */
98
    public static function getConnectionStateAsString($state)
99
    {
100
        switch ($state) {
101
            case Doctrine_Transaction::STATE_SLEEP:
102
                return "open";
103
                break;
104
            case Doctrine_Transaction::STATE_BUSY:
105
                return "busy";
106
                break;
107
            case Doctrine_Transaction::STATE_ACTIVE:
108
                return "active";
109
                break;
110
        }
111
    }
112
 
113
    /**
114
     * Generates a string representation of a connection.
115
     *
116
     * This method returns an html dump of a connection, containing state, open
117
     * transactions and loaded tables.
118
     *
119
     * @param Doctrine_Connection $connection
120
     * @return string
121
     */
122
    public static function getConnectionAsString(Doctrine_Connection $connection)
123
    {
124
        $r[] = '<pre>';
125
        $r[] = 'Doctrine_Connection object';
126
        $r[] = 'State               : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
127
        $r[] = 'Open Transactions   : ' . $connection->transaction->getTransactionLevel();
128
        $r[] = 'Table in memory     : ' . $connection->count();
129
        $r[] = 'Driver name         : ' . $connection->getAttribute(Doctrine_Core::ATTR_DRIVER_NAME);
130
        $r[] = "</pre>";
131
 
132
        return implode("\n",$r)."<br>";
133
    }
134
 
135
    /**
136
     * Generates a string representation of a table.
137
     *
138
     * This method returns an html dump of a table, containing component name
139
     * and table physical name.
140
     * @param Doctrine_Table $table
141
     * @return string
142
     */
143
    public static function getTableAsString(Doctrine_Table $table)
144
    {
145
        $r[] = "<pre>";
146
        $r[] = "Component   : ".$table->getComponentName();
147
        $r[] = "Table       : ".$table->getTableName();
148
        $r[] = "</pre>";
149
 
150
        return implode("\n",$r)."<br>";
151
    }
152
 
153
    /**
154
     * Generates a colored sql query.
155
     *
156
     * This methods parses a plain text query and generates the html needed
157
     * for visual formatting.
158
     *
159
     * @todo: What about creating a config varialbe for the color?
160
     * @param string $sql   plain text query
161
     * @return string       the formatted sql code
162
     */
163
    public static function formatSql($sql)
164
    {
165
        $e = explode("\n",$sql);
166
        $color = "367FAC";
167
        $l = $sql;
168
        $l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \>  ",$l);
169
        $l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
170
        $l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
171
        $l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
172
        $l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
173
        $l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
174
        $l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
175
        $l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \>  ",$l);
176
        $l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
177
        $l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
178
        $l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
179
        $l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
180
        $l = str_replace("  ", "<dd>",$l);
181
 
182
        return $l;
183
    }
184
 
185
    /**
186
     * Generates a string representation of a collection.
187
     *
188
     * This method returns an html dump of a collection of records, containing
189
     * all data.
190
     *
191
     * @param Doctrine_Collection $collection
192
     * @return string
193
     */
194
    public static function getCollectionAsString(Doctrine_Collection $collection)
195
    {
196
        $r[] = "<pre>";
197
        $r[] = get_class($collection);
198
        $r[] = 'data : ' . Doctrine_Core::dump($collection->getData(), false);
199
        //$r[] = 'snapshot : ' . Doctrine_Core::dump($collection->getSnapshot());
200
        $r[] = "</pre>";
201
 
202
        return implode("\n",$r);
203
    }
204
 
205
    // Code from symfony sfToolkit class. See LICENSE
206
    // code from php at moechofe dot com (array_merge comment on php.net)
207
    /*
208
     * arrayDeepMerge
209
     *
210
     * array arrayDeepMerge ( array array1 [, array array2 [, array ...]] )
211
     *
212
     * Like array_merge
213
     *
214
     *  arrayDeepMerge() merges the elements of one or more arrays together so
215
     * that the values of one are appended to the end of the previous one. It
216
     * returns the resulting array.
217
     *  If the input arrays have the same string keys, then the later value for
218
     * that key will overwrite the previous one. If, however, the arrays contain
219
     * numeric keys, the later value will not overwrite the original value, but
220
     * will be appended.
221
     *  If only one array is given and the array is numerically indexed, the keys
222
     * get reindexed in a continuous way.
223
     *
224
     * Different from array_merge
225
     *  If string keys have arrays for values, these arrays will merge recursively.
226
     */
227
     public static function arrayDeepMerge()
228
     {
229
         switch (func_num_args()) {
230
             case 0:
231
                return false;
232
             case 1:
233
                return func_get_arg(0);
234
             case 2:
235
                $args = func_get_args();
236
                $args[2] = array();
237
 
238
                if (is_array($args[0]) && is_array($args[1]))
239
                {
240
                    foreach (array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key)
241
                    {
242
                        $isKey0 = array_key_exists($key, $args[0]);
243
                        $isKey1 = array_key_exists($key, $args[1]);
244
 
245
                        if ($isKey0 && $isKey1 && is_array($args[0][$key]) && is_array($args[1][$key]))
246
                        {
247
                            $args[2][$key] = self::arrayDeepMerge($args[0][$key], $args[1][$key]);
248
                        } else if ($isKey0 && $isKey1) {
249
                            $args[2][$key] = $args[1][$key];
250
                        } else if ( ! $isKey1) {
251
                            $args[2][$key] = $args[0][$key];
252
                        } else if ( ! $isKey0) {
253
                            $args[2][$key] = $args[1][$key];
254
                        }
255
                    }
256
 
257
                    return $args[2];
258
                } else {
259
                    return $args[1];
260
                }
261
            default:
262
                $args = func_get_args();
263
                $args[1] = self::arrayDeepMerge($args[0], $args[1]);
264
                array_shift($args);
265
 
266
                return call_user_func_array(array('Doctrine_Lib', 'arrayDeepMerge'), $args);
267
            break;
268
        }
269
    }
270
 
271
    /**
272
     * Makes the directories for a path recursively.
273
     *
274
     * This method creates a given path issuing mkdir commands for all folders
275
     * that do not exist yet. Equivalent to 'mkdir -p'.
276
     *
277
     * @param string $path
278
     * @param integer $mode     an integer (octal) chmod parameter for the
279
     *                          created directories
280
     * @return boolean  true if succeeded
281
     */
282
    public static function makeDirectories($path, $mode = 0777)
283
    {
284
        if ( ! $path) {
285
          return false;
286
        }
287
 
288
        if (is_dir($path) || is_file($path)) {
289
          return true;
290
        }
291
 
292
        return mkdir(trim($path), $mode, true);
293
    }
294
 
295
    /**
296
     * Removes a non empty directory.
297
     *
298
     * This method recursively removes a directory and all its descendants.
299
     * Equivalent to 'rm -rf'.
300
     *
301
     * @param string $folderPath
302
     * @return boolean  success of the operation
303
     */
304
    public static function removeDirectories($folderPath)
305
    {
306
        if (is_dir($folderPath))
307
        {
308
            foreach (scandir($folderPath) as $value)
309
            {
310
                if ($value != '.' && $value != '..')
311
                {
312
                    $value = $folderPath . "/" . $value;
313
 
314
                    if (is_dir($value)) {
315
                        self::removeDirectories($value);
316
                    } else if (is_file($value)) {
317
                        unlink($value);
318
                    }
319
                }
320
            }
321
 
322
            return rmdir($folderPath);
323
        } else {
324
            return false;
325
        }
326
    }
327
 
328
    /**
329
     * Copy all directory content in another one.
330
     *
331
     * This method recursively copies all $source files and subdirs in $dest.
332
     * If $source is a file, only it will be copied in $dest.
333
     *
334
     * @param string $source    a directory path
335
     * @param string $dest      a directory path
336
     * @return
337
     */
338
    public static function copyDirectory($source, $dest)
339
    {
340
        // Simple copy for a file
341
        if (is_file($source)) {
342
            return copy($source, $dest);
343
        }
344
 
345
        // Make destination directory
346
        if ( ! is_dir($dest)) {
347
            mkdir($dest);
348
        }
349
 
350
        // Loop through the folder
351
        $dir = dir($source);
352
        while (false !== $entry = $dir->read()) {
353
            // Skip pointers
354
            if ($entry == '.' || $entry == '..') {
355
                continue;
356
            }
357
 
358
            // Deep copy directories
359
            if ($dest !== "$source/$entry") {
360
                self::copyDirectory("$source/$entry", "$dest/$entry");
361
            }
362
        }
363
 
364
        // Clean up
365
        $dir->close();
366
 
367
        return true;
368
    }
369
 
370
    /**
371
     * Checks for a valid class name for Doctrine coding standards.
372
     *
373
     * This methods tests if $className is a valid class name for php syntax
374
     * and for Doctrine coding standards. $className must use camel case naming
375
     * and underscores for directory separation.
376
     *
377
     * @param string $classname
378
     * @return boolean
379
     */
380
    public static function isValidClassName($className)
381
    {
382
        if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $className)) {
383
            return false;
384
        }
385
 
386
        return true;
387
    }
388
}