/usr/share/php/Nette/ComponentModel/Component.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\ComponentModel;
use Nette;
/**
* Component is the base class for all components.
*
* Components are objects implementing IComponent. They has parent component and own name.
*
* @property-read string $name
* @property-read IContainer|NULL $parent
*/
abstract class Component implements IComponent
{
use Nette\SmartObject;
/** @var IContainer */
private $parent;
/** @var string */
private $name;
/** @var array of [type => [obj, depth, path, is_monitored?]] */
private $monitors = [];
public function __construct()
{
list($parent, $name) = func_get_args() + [NULL, NULL];
if ($parent !== NULL) {
$parent->addComponent($this, $name);
} elseif (is_string($name)) {
$this->name = $name;
}
}
/**
* Lookup hierarchy for component specified by class or interface name.
* @param string class/interface type
* @param bool throw exception if component doesn't exist?
* @return IComponent
*/
public function lookup($type, $need = TRUE)
{
if (!isset($this->monitors[$type])) { // not monitored or not processed yet
$obj = $this->parent;
$path = self::NAME_SEPARATOR . $this->name;
$depth = 1;
while ($obj !== NULL) {
$parent = $obj->getParent();
if ($type ? $obj instanceof $type : $parent === NULL) {
break;
}
$path = self::NAME_SEPARATOR . $obj->getName() . $path;
$depth++;
$obj = $parent; // IComponent::getParent()
if ($obj === $this) {
$obj = NULL; // prevent cycling
}
}
if ($obj) {
$this->monitors[$type] = [$obj, $depth, substr($path, 1), FALSE];
} else {
$this->monitors[$type] = [NULL, NULL, NULL, FALSE]; // not found
}
}
if ($need && $this->monitors[$type][0] === NULL) {
throw new Nette\InvalidStateException("Component '$this->name' is not attached to '$type'.");
}
return $this->monitors[$type][0];
}
/**
* Lookup for component specified by class or interface name. Returns backtrace path.
* A path is the concatenation of component names separated by self::NAME_SEPARATOR.
* @param string class/interface type
* @param bool throw exception if component doesn't exist?
* @return string
*/
public function lookupPath($type = NULL, $need = TRUE)
{
$this->lookup($type, $need);
return $this->monitors[$type][2];
}
/**
* Starts monitoring.
* @param string class/interface type
* @return void
*/
public function monitor($type)
{
if (empty($this->monitors[$type][3])) {
if ($obj = $this->lookup($type, FALSE)) {
$this->attached($obj);
}
$this->monitors[$type][3] = TRUE; // mark as monitored
}
}
/**
* Stops monitoring.
* @param string class/interface type
* @return void
*/
public function unmonitor($type)
{
unset($this->monitors[$type]);
}
/**
* This method will be called when the component (or component's parent)
* becomes attached to a monitored object. Do not call this method yourself.
* @param IComponent
* @return void
*/
protected function attached($obj)
{
}
/**
* This method will be called before the component (or component's parent)
* becomes detached from a monitored object. Do not call this method yourself.
* @param IComponent
* @return void
*/
protected function detached($obj)
{
}
/********************* interface IComponent ****************d*g**/
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns the container if any.
* @return IContainer|NULL
*/
public function getParent()
{
return $this->parent;
}
/**
* Sets the parent of this component. This method is managed by containers and should
* not be called by applications
* @param IContainer New parent or null if this component is being removed from a parent
* @param string
* @return self
* @throws Nette\InvalidStateException
* @internal
*/
public function setParent(IContainer $parent = NULL, $name = NULL)
{
if ($parent === NULL && $this->parent === NULL && $name !== NULL) {
$this->name = $name; // just rename
return $this;
} elseif ($parent === $this->parent && $name === NULL) {
return $this; // nothing to do
}
// A component cannot be given a parent if it already has a parent.
if ($this->parent !== NULL && $parent !== NULL) {
throw new Nette\InvalidStateException("Component '$this->name' already has a parent.");
}
// remove from parent?
if ($parent === NULL) {
$this->refreshMonitors(0);
$this->parent = NULL;
} else { // add to parent
$this->validateParent($parent);
$this->parent = $parent;
if ($name !== NULL) {
$this->name = $name;
}
$tmp = [];
$this->refreshMonitors(0, $tmp);
}
return $this;
}
/**
* Is called by a component when it is about to be set new parent. Descendant can
* override this method to disallow a parent change by throwing an Nette\InvalidStateException
* @return void
* @throws Nette\InvalidStateException
*/
protected function validateParent(IContainer $parent)
{
}
/**
* Refreshes monitors.
* @param int
* @param array|NULL (array = attaching, NULL = detaching)
* @param array
* @return void
*/
private function refreshMonitors($depth, & $missing = NULL, & $listeners = [])
{
if ($this instanceof IContainer) {
foreach ($this->getComponents() as $component) {
if ($component instanceof self) {
$component->refreshMonitors($depth + 1, $missing, $listeners);
}
}
}
if ($missing === NULL) { // detaching
foreach ($this->monitors as $type => $rec) {
if (isset($rec[1]) && $rec[1] > $depth) {
if ($rec[3]) { // monitored
$this->monitors[$type] = [NULL, NULL, NULL, TRUE];
$listeners[] = [$this, $rec[0]];
} else { // not monitored, just randomly cached
unset($this->monitors[$type]);
}
}
}
} else { // attaching
foreach ($this->monitors as $type => $rec) {
if (isset($rec[0])) { // is in cache yet
continue;
} elseif (!$rec[3]) { // not monitored, just randomly cached
unset($this->monitors[$type]);
} elseif (isset($missing[$type])) { // known from previous lookup
$this->monitors[$type] = [NULL, NULL, NULL, TRUE];
} else {
$this->monitors[$type] = NULL; // forces re-lookup
if ($obj = $this->lookup($type, FALSE)) {
$listeners[] = [$this, $obj];
} else {
$missing[$type] = TRUE;
}
$this->monitors[$type][3] = TRUE; // mark as monitored
}
}
}
if ($depth === 0) { // call listeners
$method = $missing === NULL ? 'detached' : 'attached';
$prev = [];
foreach ($listeners as $item) {
if (!in_array($item, $prev, TRUE)) {
$item[0]->$method($item[1]);
$prev[] = $item;
}
}
}
}
/********************* cloneable, serializable ****************d*g**/
/**
* Object cloning.
*/
public function __clone()
{
if ($this->parent === NULL) {
return;
} elseif ($this->parent instanceof Container) {
$this->parent = $this->parent->_isCloning();
if ($this->parent === NULL) { // not cloning
$this->refreshMonitors(0);
}
} else {
$this->parent = NULL;
$this->refreshMonitors(0);
}
}
/**
* Prevents serialization.
*/
public function __sleep()
{
throw new Nette\NotImplementedException('Object serialization is not supported by class ' . get_class($this));
}
/**
* Prevents unserialization.
*/
public function __wakeup()
{
throw new Nette\NotImplementedException('Object unserialization is not supported by class ' . get_class($this));
}
}
|