/usr/share/php/Doctrine/Common/Proxy/ProxyGenerator.php is in php-doctrine-common 2.4.3-1ubuntu1.
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 | <?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Proxy;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
/**
* This factory is used to generate proxy classes.
* It builds proxies from given parameters, a template and class metadata.
*
* @author Marco Pivetta <ocramius@gmail.com>
* @since 2.4
*/
class ProxyGenerator
{
/**
* Used to match very simple id methods that don't need
* to be decorated since the identifier is known.
*/
const PATTERN_MATCH_ID_METHOD = '((public\s)?(function\s{1,}%s\s?\(\)\s{1,})\s{0,}{\s{0,}return\s{0,}\$this->%s;\s{0,}})i';
/**
* The namespace that contains all proxy classes.
*
* @var string
*/
private $proxyNamespace;
/**
* The directory that contains all proxy classes.
*
* @var string
*/
private $proxyDirectory;
/**
* Map of callables used to fill in placeholders set in the template.
*
* @var string[]|callable[]
*/
protected $placeholders = array(
'baseProxyInterface' => 'Doctrine\Common\Proxy\Proxy',
'additionalProperties' => '',
);
/**
* Template used as a blueprint to generate proxies.
*
* @var string
*/
protected $proxyClassTemplate = '<?php
namespace <namespace>;
/**
* DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE\'S PROXY GENERATOR
*/
class <proxyShortClassName> extends \<className> implements \<baseProxyInterface>
{
/**
* @var \Closure the callback responsible for loading properties in the proxy object. This callback is called with
* three parameters, being respectively the proxy object to be initialized, the method that triggered the
* initialization process and an array of ordered parameters that were passed to that method.
*
* @see \Doctrine\Common\Persistence\Proxy::__setInitializer
*/
public $__initializer__;
/**
* @var \Closure the callback responsible of loading properties that need to be copied in the cloned object
*
* @see \Doctrine\Common\Persistence\Proxy::__setCloner
*/
public $__cloner__;
/**
* @var boolean flag indicating if this object was already initialized
*
* @see \Doctrine\Common\Persistence\Proxy::__isInitialized
*/
public $__isInitialized__ = false;
/**
* @var array properties to be lazy loaded, with keys being the property
* names and values being their default values
*
* @see \Doctrine\Common\Persistence\Proxy::__getLazyProperties
*/
public static $lazyPropertiesDefaults = array(<lazyPropertiesDefaults>);
<additionalProperties>
<constructorImpl>
<magicGet>
<magicSet>
<magicIsset>
<sleepImpl>
<wakeupImpl>
<cloneImpl>
/**
* Forces initialization of the proxy
*/
public function __load()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, \'__load\', array());
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
*/
public function __isInitialized()
{
return $this->__isInitialized__;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
*/
public function __setInitialized($initialized)
{
$this->__isInitialized__ = $initialized;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
*/
public function __setInitializer(\Closure $initializer = null)
{
$this->__initializer__ = $initializer;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
*/
public function __getInitializer()
{
return $this->__initializer__;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
*/
public function __setCloner(\Closure $cloner = null)
{
$this->__cloner__ = $cloner;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific cloning logic
*/
public function __getCloner()
{
return $this->__cloner__;
}
/**
* {@inheritDoc}
* @internal generated method: use only when explicitly handling proxy specific loading logic
* @static
*/
public function __getLazyProperties()
{
return self::$lazyPropertiesDefaults;
}
<methods>
}
';
/**
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
* connected to the given <tt>EntityManager</tt>.
*
* @param string $proxyDirectory The directory to use for the proxy classes. It must exist.
* @param string $proxyNamespace The namespace to use for the proxy classes.
*
* @throws InvalidArgumentException
*/
public function __construct($proxyDirectory, $proxyNamespace)
{
if ( ! $proxyDirectory) {
throw InvalidArgumentException::proxyDirectoryRequired();
}
if ( ! $proxyNamespace) {
throw InvalidArgumentException::proxyNamespaceRequired();
}
$this->proxyDirectory = $proxyDirectory;
$this->proxyNamespace = $proxyNamespace;
}
/**
* Sets a placeholder to be replaced in the template.
*
* @param string $name
* @param string|callable $placeholder
*
* @throws InvalidArgumentException
*/
public function setPlaceholder($name, $placeholder)
{
if ( ! is_string($placeholder) && ! is_callable($placeholder)) {
throw InvalidArgumentException::invalidPlaceholder($name);
}
$this->placeholders[$name] = $placeholder;
}
/**
* Sets the base template used to create proxy classes.
*
* @param string $proxyClassTemplate
*/
public function setProxyClassTemplate($proxyClassTemplate)
{
$this->proxyClassTemplate = (string) $proxyClassTemplate;
}
/**
* Generates a proxy class file.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class Metadata for the original class.
* @param string|bool $fileName Filename (full path) for the generated class. If none is given, eval() is used.
*
* @throws UnexpectedValueException
*/
public function generateProxyClass(ClassMetadata $class, $fileName = false)
{
preg_match_all('(<([a-zA-Z]+)>)', $this->proxyClassTemplate, $placeholderMatches);
$placeholderMatches = array_combine($placeholderMatches[0], $placeholderMatches[1]);
$placeholders = array();
foreach ($placeholderMatches as $placeholder => $name) {
$placeholders[$placeholder] = isset($this->placeholders[$name])
? $this->placeholders[$name]
: array($this, 'generate' . $name);
}
foreach ($placeholders as & $placeholder) {
if (is_callable($placeholder)) {
$placeholder = call_user_func($placeholder, $class);
}
}
$proxyCode = strtr($this->proxyClassTemplate, $placeholders);
if ( ! $fileName) {
$proxyClassName = $this->generateNamespace($class) . '\\' . $this->generateProxyShortClassName($class);
if ( ! class_exists($proxyClassName)) {
eval(substr($proxyCode, 5));
}
return;
}
$parentDirectory = dirname($fileName);
if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) {
throw UnexpectedValueException::proxyDirectoryNotWritable($this->proxyDirectory);
}
if ( ! is_writable($parentDirectory)) {
throw UnexpectedValueException::proxyDirectoryNotWritable($this->proxyDirectory);
}
$tmpFileName = $fileName . '.' . uniqid('', true);
file_put_contents($tmpFileName, $proxyCode);
chmod($tmpFileName, 0664);
rename($tmpFileName, $fileName);
}
/**
* Generates the proxy short class name to be used in the template.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateProxyShortClassName(ClassMetadata $class)
{
$proxyClassName = ClassUtils::generateProxyClassName($class->getName(), $this->proxyNamespace);
$parts = explode('\\', strrev($proxyClassName), 2);
return strrev($parts[0]);
}
/**
* Generates the proxy namespace.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateNamespace(ClassMetadata $class)
{
$proxyClassName = ClassUtils::generateProxyClassName($class->getName(), $this->proxyNamespace);
$parts = explode('\\', strrev($proxyClassName), 2);
return strrev($parts[1]);
}
/**
* Generates the original class name.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateClassName(ClassMetadata $class)
{
return ltrim($class->getName(), '\\');
}
/**
* Generates the array representation of lazy loaded public properties and their default values.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateLazyPropertiesDefaults(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicProperties($class);
$values = array();
foreach ($lazyPublicProperties as $key => $value) {
$values[] = var_export($key, true) . ' => ' . var_export($value, true);
}
return implode(', ', $values);
}
/**
* Generates the constructor code (un-setting public lazy loaded properties, setting identifier field values).
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateConstructorImpl(ClassMetadata $class)
{
$constructorImpl = <<<'EOT'
/**
* @param \Closure $initializer
* @param \Closure $cloner
*/
public function __construct($initializer = null, $cloner = null)
{
EOT;
$toUnset = array();
foreach ($this->getLazyLoadedPublicProperties($class) as $lazyPublicProperty => $unused) {
$toUnset[] = '$this->' . $lazyPublicProperty;
}
$constructorImpl .= (empty($toUnset) ? '' : ' unset(' . implode(', ', $toUnset) . ");\n")
. <<<'EOT'
$this->__initializer__ = $initializer;
$this->__cloner__ = $cloner;
}
EOT;
return $constructorImpl;
}
/**
* Generates the magic getter invoked when lazy loaded public properties are requested.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateMagicGet(ClassMetadata $class)
{
$lazyPublicProperties = array_keys($this->getLazyLoadedPublicProperties($class));
$reflectionClass = $class->getReflectionClass();
$hasParentGet = false;
$returnReference = '';
$inheritDoc = '';
if ($reflectionClass->hasMethod('__get')) {
$hasParentGet = true;
$inheritDoc = '{@inheritDoc}';
if ($reflectionClass->getMethod('__get')->returnsReference()) {
$returnReference = '& ';
}
}
if (empty($lazyPublicProperties) && ! $hasParentGet) {
return '';
}
$magicGet = <<<EOT
/**
* $inheritDoc
* @param string \$name
*/
public function {$returnReference}__get(\$name)
{
EOT;
if ( ! empty($lazyPublicProperties)) {
$magicGet .= <<<'EOT'
if (array_key_exists($name, $this->__getLazyProperties())) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', array($name));
return $this->$name;
}
EOT;
}
if ($hasParentGet) {
$magicGet .= <<<'EOT'
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', array($name));
return parent::__get($name);
EOT;
} else {
$magicGet .= <<<'EOT'
trigger_error(sprintf('Undefined property: %s::$%s', __CLASS__, $name), E_USER_NOTICE);
EOT;
}
$magicGet .= " }";
return $magicGet;
}
/**
* Generates the magic setter (currently unused).
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateMagicSet(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicProperties($class);
$hasParentSet = $class->getReflectionClass()->hasMethod('__set');
if (empty($lazyPublicProperties) && ! $hasParentSet) {
return '';
}
$inheritDoc = $hasParentSet ? '{@inheritDoc}' : '';
$magicSet = <<<EOT
/**
* $inheritDoc
* @param string \$name
* @param mixed \$value
*/
public function __set(\$name, \$value)
{
EOT;
if ( ! empty($lazyPublicProperties)) {
$magicSet .= <<<'EOT'
if (array_key_exists($name, $this->__getLazyProperties())) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__set', array($name, $value));
$this->$name = $value;
return;
}
EOT;
}
if ($hasParentSet) {
$magicSet .= <<<'EOT'
$this->__initializer__ && $this->__initializer__->__invoke($this, '__set', array($name, $value));
return parent::__set($name, $value);
EOT;
} else {
$magicSet .= " \$this->\$name = \$value;";
}
$magicSet .= "\n }";
return $magicSet;
}
/**
* Generates the magic issetter invoked when lazy loaded public properties are checked against isset().
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateMagicIsset(ClassMetadata $class)
{
$lazyPublicProperties = array_keys($this->getLazyLoadedPublicProperties($class));
$hasParentIsset = $class->getReflectionClass()->hasMethod('__isset');
if (empty($lazyPublicProperties) && ! $hasParentIsset) {
return '';
}
$inheritDoc = $hasParentIsset ? '{@inheritDoc}' : '';
$magicIsset = <<<EOT
/**
* $inheritDoc
* @param string \$name
* @return boolean
*/
public function __isset(\$name)
{
EOT;
if ( ! empty($lazyPublicProperties)) {
$magicIsset .= <<<'EOT'
if (array_key_exists($name, $this->__getLazyProperties())) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__isset', array($name));
return isset($this->$name);
}
EOT;
}
if ($hasParentIsset) {
$magicIsset .= <<<'EOT'
$this->__initializer__ && $this->__initializer__->__invoke($this, '__isset', array($name));
return parent::__isset($name);
EOT;
} else {
$magicIsset .= " return false;";
}
return $magicIsset . "\n }";
}
/**
* Generates implementation for the `__sleep` method of proxies.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateSleepImpl(ClassMetadata $class)
{
$hasParentSleep = $class->getReflectionClass()->hasMethod('__sleep');
$inheritDoc = $hasParentSleep ? '{@inheritDoc}' : '';
$sleepImpl = <<<EOT
/**
* $inheritDoc
* @return array
*/
public function __sleep()
{
EOT;
if ($hasParentSleep) {
return $sleepImpl . <<<'EOT'
$properties = array_merge(array('__isInitialized__'), parent::__sleep());
if ($this->__isInitialized__) {
$properties = array_diff($properties, array_keys($this->__getLazyProperties()));
}
return $properties;
}
EOT;
}
$allProperties = array('__isInitialized__');
/* @var $prop \ReflectionProperty */
foreach ($class->getReflectionClass()->getProperties() as $prop) {
if ($prop->isStatic()) {
continue;
}
$allProperties[] = $prop->isPrivate()
? "\0" . $prop->getDeclaringClass()->getName() . "\0" . $prop->getName()
: $prop->getName();
}
$lazyPublicProperties = array_keys($this->getLazyLoadedPublicProperties($class));
$protectedProperties = array_diff($allProperties, $lazyPublicProperties);
foreach ($allProperties as &$property) {
$property = var_export($property, true);
}
foreach ($protectedProperties as &$property) {
$property = var_export($property, true);
}
$allProperties = implode(', ', $allProperties);
$protectedProperties = implode(', ', $protectedProperties);
return $sleepImpl . <<<EOT
if (\$this->__isInitialized__) {
return array($allProperties);
}
return array($protectedProperties);
}
EOT;
}
/**
* Generates implementation for the `__wakeup` method of proxies.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateWakeupImpl(ClassMetadata $class)
{
$unsetPublicProperties = array();
$hasWakeup = $class->getReflectionClass()->hasMethod('__wakeup');
foreach (array_keys($this->getLazyLoadedPublicProperties($class)) as $lazyPublicProperty) {
$unsetPublicProperties[] = '$this->' . $lazyPublicProperty;
}
$shortName = $this->generateProxyShortClassName($class);
$inheritDoc = $hasWakeup ? '{@inheritDoc}' : '';
$wakeupImpl = <<<EOT
/**
* $inheritDoc
*/
public function __wakeup()
{
if ( ! \$this->__isInitialized__) {
\$this->__initializer__ = function ($shortName \$proxy) {
\$proxy->__setInitializer(null);
\$proxy->__setCloner(null);
\$existingProperties = get_object_vars(\$proxy);
foreach (\$proxy->__getLazyProperties() as \$property => \$defaultValue) {
if ( ! array_key_exists(\$property, \$existingProperties)) {
\$proxy->\$property = \$defaultValue;
}
}
};
EOT;
if ( ! empty($unsetPublicProperties)) {
$wakeupImpl .= "\n unset(" . implode(', ', $unsetPublicProperties) . ");";
}
$wakeupImpl .= "\n }";
if ($hasWakeup) {
$wakeupImpl .= "\n parent::__wakeup();";
}
$wakeupImpl .= "\n }";
return $wakeupImpl;
}
/**
* Generates implementation for the `__clone` method of proxies.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateCloneImpl(ClassMetadata $class)
{
$hasParentClone = $class->getReflectionClass()->hasMethod('__clone');
$inheritDoc = $hasParentClone ? '{@inheritDoc}' : '';
$callParentClone = $hasParentClone ? "\n parent::__clone();\n" : '';
return <<<EOT
/**
* $inheritDoc
*/
public function __clone()
{
\$this->__cloner__ && \$this->__cloner__->__invoke(\$this, '__clone', array());
$callParentClone }
EOT;
}
/**
* Generates decorated methods by picking those available in the parent class.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateMethods(ClassMetadata $class)
{
$methods = '';
$methodNames = array();
$reflectionMethods = $class->getReflectionClass()->getMethods(\ReflectionMethod::IS_PUBLIC);
$skippedMethods = array(
'__sleep' => true,
'__clone' => true,
'__wakeup' => true,
'__get' => true,
'__set' => true,
'__isset' => true,
);
foreach ($reflectionMethods as $method) {
$name = $method->getName();
if (
$method->isConstructor() ||
isset($skippedMethods[strtolower($name)]) ||
isset($methodNames[$name]) ||
$method->isFinal() ||
$method->isStatic() ||
( ! $method->isPublic())
) {
continue;
}
$methodNames[$name] = true;
$methods .= "\n /**\n"
. " * {@inheritDoc}\n"
. " */\n"
. ' public function ';
if ($method->returnsReference()) {
$methods .= '&';
}
$methods .= $name . '(';
$firstParam = true;
$parameterString = '';
$argumentString = '';
$parameters = array();
foreach ($method->getParameters() as $param) {
if ($firstParam) {
$firstParam = false;
} else {
$parameterString .= ', ';
$argumentString .= ', ';
}
try {
$paramClass = $param->getClass();
} catch (\ReflectionException $previous) {
throw UnexpectedValueException::invalidParameterTypeHint(
$class->getName(),
$method->getName(),
$param->getName(),
$previous
);
}
// We need to pick the type hint class too
if (null !== $paramClass) {
$parameterString .= '\\' . $paramClass->getName() . ' ';
} elseif ($param->isArray()) {
$parameterString .= 'array ';
} elseif (method_exists($param, 'isCallable') && $param->isCallable()) {
$parameterString .= 'callable ';
}
if ($param->isPassedByReference()) {
$parameterString .= '&';
}
$parameters[] = '$' . $param->getName();
$parameterString .= '$' . $param->getName();
$argumentString .= '$' . $param->getName();
if ($param->isDefaultValueAvailable()) {
$parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
}
}
$methods .= $parameterString . ')';
$methods .= "\n" . ' {' . "\n";
if ($this->isShortIdentifierGetter($method, $class)) {
$identifier = lcfirst(substr($name, 3));
$fieldType = $class->getTypeOfField($identifier);
$cast = in_array($fieldType, array('integer', 'smallint')) ? '(int) ' : '';
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
$methods .= ' return ' . $cast . ' parent::' . $method->getName() . "();\n";
$methods .= ' }' . "\n\n";
}
$methods .= "\n \$this->__initializer__ "
. "&& \$this->__initializer__->__invoke(\$this, " . var_export($name, true)
. ", array(" . implode(', ', $parameters) . "));"
. "\n\n return parent::" . $name . '(' . $argumentString . ');'
. "\n" . ' }' . "\n";
}
return $methods;
}
/**
* Generates the Proxy file name.
*
* @param string $className
* @param string $baseDirectory Optional base directory for proxy file name generation.
* If not specified, the directory configured on the Configuration of the
* EntityManager will be used by this factory.
*
* @return string
*/
public function getProxyFileName($className, $baseDirectory = null)
{
$baseDirectory = $baseDirectory ?: $this->proxyDirectory;
return rtrim($baseDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . Proxy::MARKER
. str_replace('\\', '', $className) . '.php';
}
/**
* Checks if the method is a short identifier getter.
*
* What does this mean? For proxy objects the identifier is already known,
* however accessing the getter for this identifier usually triggers the
* lazy loading, leading to a query that may not be necessary if only the
* ID is interesting for the userland code (for example in views that
* generate links to the entity, but do not display anything else).
*
* @param \ReflectionMethod $method
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return boolean
*/
private function isShortIdentifierGetter($method, ClassMetadata $class)
{
$identifier = lcfirst(substr($method->getName(), 3));
$startLine = $method->getStartLine();
$endLine = $method->getEndLine();
$cheapCheck = (
$method->getNumberOfParameters() == 0
&& substr($method->getName(), 0, 3) == 'get'
&& in_array($identifier, $class->getIdentifier(), true)
&& $class->hasField($identifier)
&& (($endLine - $startLine) <= 4)
);
if ($cheapCheck) {
$code = file($method->getDeclaringClass()->getFileName());
$code = trim(implode(' ', array_slice($code, $startLine - 1, $endLine - $startLine + 1)));
$pattern = sprintf(self::PATTERN_MATCH_ID_METHOD, $method->getName(), $identifier);
if (preg_match($pattern, $code)) {
return true;
}
}
return false;
}
/**
* Generates the list of public properties to be lazy loaded, with their default values.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return mixed[]
*/
private function getLazyLoadedPublicProperties(ClassMetadata $class)
{
$defaultProperties = $class->getReflectionClass()->getDefaultProperties();
$properties = array();
foreach ($class->getReflectionClass()->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
if (($class->hasField($name) || $class->hasAssociation($name)) && ! $class->isIdentifier($name)) {
$properties[$name] = $defaultProperties[$name];
}
}
return $properties;
}
}
|