db backup prior to drupal security update
[yaffs-website] / vendor / symfony / var-dumper / Cloner / Data.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Cloner;
13
14 /**
15  * @author Nicolas Grekas <p@tchwork.com>
16  */
17 class Data
18 {
19     private $data;
20     private $maxDepth = 20;
21     private $maxItemsPerDepth = -1;
22     private $useRefHandles = -1;
23
24     /**
25      * @param array $data A array as returned by ClonerInterface::cloneVar()
26      */
27     public function __construct(array $data)
28     {
29         $this->data = $data;
30     }
31
32     /**
33      * @return array The raw data structure
34      */
35     public function getRawData()
36     {
37         return $this->data;
38     }
39
40     /**
41      * Returns a depth limited clone of $this.
42      *
43      * @param int $maxDepth The max dumped depth level
44      *
45      * @return self A clone of $this
46      */
47     public function withMaxDepth($maxDepth)
48     {
49         $data = clone $this;
50         $data->maxDepth = (int) $maxDepth;
51
52         return $data;
53     }
54
55     /**
56      * Limits the number of elements per depth level.
57      *
58      * @param int $maxItemsPerDepth The max number of items dumped per depth level
59      *
60      * @return self A clone of $this
61      */
62     public function withMaxItemsPerDepth($maxItemsPerDepth)
63     {
64         $data = clone $this;
65         $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
66
67         return $data;
68     }
69
70     /**
71      * Enables/disables objects' identifiers tracking.
72      *
73      * @param bool $useRefHandles False to hide global ref. handles
74      *
75      * @return self A clone of $this
76      */
77     public function withRefHandles($useRefHandles)
78     {
79         $data = clone $this;
80         $data->useRefHandles = $useRefHandles ? -1 : 0;
81
82         return $data;
83     }
84
85     /**
86      * Returns a depth limited clone of $this.
87      *
88      * @param int  $maxDepth         The max dumped depth level
89      * @param int  $maxItemsPerDepth The max number of items dumped per depth level
90      * @param bool $useRefHandles    False to hide ref. handles
91      *
92      * @return self A depth limited clone of $this
93      *
94      * @deprecated since Symfony 2.7, to be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles instead.
95      */
96     public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
97     {
98         @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles methods instead.', E_USER_DEPRECATED);
99
100         $data = clone $this;
101         $data->maxDepth = (int) $maxDepth;
102         $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
103         $data->useRefHandles = $useRefHandles ? -1 : 0;
104
105         return $data;
106     }
107
108     /**
109      * Dumps data with a DumperInterface dumper.
110      */
111     public function dump(DumperInterface $dumper)
112     {
113         $refs = array(0);
114         $this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
115     }
116
117     /**
118      * Depth-first dumping of items.
119      *
120      * @param DumperInterface $dumper The dumper being used for dumping
121      * @param Cursor          $cursor A cursor used for tracking dumper state position
122      * @param array           &$refs  A map of all references discovered while dumping
123      * @param mixed           $item   A Stub object or the original value being dumped
124      */
125     private function dumpItem($dumper, $cursor, &$refs, $item)
126     {
127         $cursor->refIndex = 0;
128         $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
129         $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
130         $firstSeen = true;
131
132         if (!$item instanceof Stub) {
133             $type = gettype($item);
134         } elseif (Stub::TYPE_REF === $item->type) {
135             if ($item->handle) {
136                 if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
137                     $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
138                 } else {
139                     $firstSeen = false;
140                 }
141                 $cursor->hardRefTo = $refs[$r];
142                 $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
143                 $cursor->hardRefCount = $item->refCount;
144             }
145             $type = $item->class ?: gettype($item->value);
146             $item = $item->value;
147         }
148         if ($item instanceof Stub) {
149             if ($item->refCount) {
150                 if (!isset($refs[$r = $item->handle])) {
151                     $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
152                 } else {
153                     $firstSeen = false;
154                 }
155                 $cursor->softRefTo = $refs[$r];
156             }
157             $cursor->softRefHandle = $this->useRefHandles & $item->handle;
158             $cursor->softRefCount = $item->refCount;
159             $cut = $item->cut;
160
161             if ($item->position && $firstSeen) {
162                 $children = $this->data[$item->position];
163
164                 if ($cursor->stop) {
165                     if ($cut >= 0) {
166                         $cut += count($children);
167                     }
168                     $children = array();
169                 }
170             } else {
171                 $children = array();
172             }
173             switch ($item->type) {
174                 case Stub::TYPE_STRING:
175                     $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
176                     break;
177
178                 case Stub::TYPE_ARRAY:
179                     $item = clone $item;
180                     $item->type = $item->class;
181                     $item->class = $item->value;
182                     // No break;
183                 case Stub::TYPE_OBJECT:
184                 case Stub::TYPE_RESOURCE:
185                     $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
186                     $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
187                     if ($withChildren) {
188                         $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
189                     } elseif ($children && 0 <= $cut) {
190                         $cut += count($children);
191                     }
192                     $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
193                     break;
194
195                 default:
196                     throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
197             }
198         } elseif ('array' === $type) {
199             $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
200             $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
201         } elseif ('string' === $type) {
202             $dumper->dumpString($cursor, $item, false, 0);
203         } else {
204             $dumper->dumpScalar($cursor, $type, $item);
205         }
206     }
207
208     /**
209      * Dumps children of hash structures.
210      *
211      * @param DumperInterface $dumper
212      * @param Cursor          $parentCursor The cursor of the parent hash
213      * @param array           &$refs        A map of all references discovered while dumping
214      * @param array           $children     The children to dump
215      * @param int             $hashCut      The number of items removed from the original hash
216      * @param string          $hashType     A Cursor::HASH_* const
217      *
218      * @return int The final number of removed items
219      */
220     private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
221     {
222         $cursor = clone $parentCursor;
223         ++$cursor->depth;
224         $cursor->hashType = $hashType;
225         $cursor->hashIndex = 0;
226         $cursor->hashLength = count($children);
227         $cursor->hashCut = $hashCut;
228         foreach ($children as $key => $child) {
229             $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
230             $cursor->hashKey = $key;
231             $this->dumpItem($dumper, $cursor, $refs, $child);
232             if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
233                 $parentCursor->stop = true;
234
235                 return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
236             }
237         }
238
239         return $hashCut;
240     }
241 }