|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Contao Debugger |
| 4 | + * |
| 5 | + * Copyright (c) 2014 Christian Schiffler |
| 6 | + * |
| 7 | + * @package ContaoDebugger |
| 8 | + * @author Christian Schiffler <[email protected]> |
| 9 | + * @copyright 2014 CyberSpectrum |
| 10 | + * @license LGPL-3+ |
| 11 | + * @filesource |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CyberSpectrum\ContaoDebugger\Database; |
| 15 | + |
| 16 | +use CyberSpectrum\ContaoDebugger\DebugBar\DataCollector\ContaoSQLCollector; |
| 17 | + |
| 18 | +/** |
| 19 | + * Debugger delegator for Contao Database. |
| 20 | + */ |
| 21 | +class DatabaseDebugger extends \ArrayObject |
| 22 | +{ |
| 23 | + /** |
| 24 | + * The database collector in use. |
| 25 | + * |
| 26 | + * @var ContaoSQLCollector |
| 27 | + */ |
| 28 | + protected $collector = null; |
| 29 | + |
| 30 | + /** |
| 31 | + * Attach the debugger to the Contao Database. |
| 32 | + * |
| 33 | + * @param ContaoSQLCollector $collector The SQL collector to use. |
| 34 | + * |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public static function attach(ContaoSQLCollector $collector = null) |
| 38 | + { |
| 39 | + static $attached; |
| 40 | + |
| 41 | + if (isset($attached)) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $watcher = new self($collector); |
| 47 | + |
| 48 | + $reflection = new \ReflectionClass('\Database'); |
| 49 | + $property = $reflection->getProperty('arrInstances'); |
| 50 | + $property->setAccessible(true); |
| 51 | + $property->setValue($watcher); |
| 52 | + |
| 53 | + $reflection = new \ReflectionClass('\Contao\Database'); |
| 54 | + $property = $reflection->getProperty('arrInstances'); |
| 55 | + $property->setAccessible(true); |
| 56 | + $property->setValue($watcher); |
| 57 | + |
| 58 | + $attached = true; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Log a statement. |
| 63 | + * |
| 64 | + * @param array $info The information. |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function addStatement($info) |
| 69 | + { |
| 70 | + if ($this->collector) |
| 71 | + { |
| 72 | + $this->collector->addStatement($info); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a new instance. |
| 78 | + * |
| 79 | + * @param ContaoSQLCollector $collector The SQL collector to use. |
| 80 | + */ |
| 81 | + public function __construct(ContaoSQLCollector $collector = null) |
| 82 | + { |
| 83 | + parent::__construct(array(), (self::STD_PROP_LIST | self::ARRAY_AS_PROPS)); |
| 84 | + $this->collector = $collector; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get an offset in the array object. |
| 89 | + * |
| 90 | + * @param string $index The hash key of the database to be fetched. |
| 91 | + * |
| 92 | + * @return mixed |
| 93 | + */ |
| 94 | + public function offsetGet($index) |
| 95 | + { |
| 96 | + $val = parent::offsetGet($index); |
| 97 | + return $val; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Set an offset in the array object. |
| 102 | + * |
| 103 | + * @param string $index The hash key of the database to be set. |
| 104 | + * |
| 105 | + * @param \Database $newVal The new Database instance. |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function offsetSet($index, $newVal) |
| 110 | + { |
| 111 | + $val = new DatabaseDelegator($newVal, $this); |
| 112 | + |
| 113 | + parent::offsetSet($index, $val); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns whether the requested database connection exists. |
| 118 | + * |
| 119 | + * @param string $index The hash key of the database to be checked. |
| 120 | + * |
| 121 | + * @return bool true if the requested database exists, otherwise false. |
| 122 | + */ |
| 123 | + public function offsetExists($index) |
| 124 | + { |
| 125 | + $val = parent::offsetExists($index); |
| 126 | + return $val; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Unregister the database with the given hash key. |
| 131 | + * |
| 132 | + * @param string $index The hash key of the database to be checked. |
| 133 | + * |
| 134 | + * @return void |
| 135 | + */ |
| 136 | + // @codingStandardsIgnoreStart - The override is not useless, we need it for the phpDoc. |
| 137 | + public function offsetUnset($index) |
| 138 | + { |
| 139 | + parent::offsetUnset($index); |
| 140 | + } |
| 141 | + // @codingStandardsIgnoreEnd |
| 142 | +} |
0 commit comments