[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/ -> Definition.php (source)

   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\DependencyInjection;
  13  
  14  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15  use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
  16  
  17  /**
  18   * Definition represents a service definition.
  19   *
  20   * @author Fabien Potencier <fabien@symfony.com>
  21   */
  22  class Definition
  23  {
  24      private $class;
  25      private $file;
  26      private $factory;
  27      private $factoryClass;
  28      private $factoryMethod;
  29      private $factoryService;
  30      private $shared = true;
  31      private $deprecated = false;
  32      private $deprecationTemplate;
  33      private $scope = ContainerInterface::SCOPE_CONTAINER;
  34      private $properties = array();
  35      private $calls = array();
  36      private $configurator;
  37      private $tags = array();
  38      private $public = true;
  39      private $synthetic = false;
  40      private $abstract = false;
  41      private $synchronized = false;
  42      private $lazy = false;
  43      private $decoratedService;
  44      private $autowired = false;
  45      private $autowiringTypes = array();
  46  
  47      private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
  48  
  49      protected $arguments;
  50  
  51      /**
  52       * @param string|null $class     The service class
  53       * @param array       $arguments An array of arguments to pass to the service constructor
  54       */
  55      public function __construct($class = null, array $arguments = array())
  56      {
  57          $this->class = $class;
  58          $this->arguments = $arguments;
  59      }
  60  
  61      /**
  62       * Sets a factory.
  63       *
  64       * @param string|array $factory A PHP function or an array containing a class/Reference and a method to call
  65       *
  66       * @return $this
  67       */
  68      public function setFactory($factory)
  69      {
  70          if (\is_string($factory) && false !== strpos($factory, '::')) {
  71              $factory = explode('::', $factory, 2);
  72          }
  73  
  74          $this->factory = $factory;
  75  
  76          return $this;
  77      }
  78  
  79      /**
  80       * Gets the factory.
  81       *
  82       * @return string|array|null The PHP function or an array containing a class/Reference and a method to call
  83       */
  84      public function getFactory()
  85      {
  86          return $this->factory;
  87      }
  88  
  89      /**
  90       * Sets the name of the class that acts as a factory using the factory method,
  91       * which will be invoked statically.
  92       *
  93       * @param string $factoryClass The factory class name
  94       *
  95       * @return $this
  96       *
  97       * @deprecated since version 2.6, to be removed in 3.0.
  98       */
  99      public function setFactoryClass($factoryClass)
 100      {
 101          @trigger_error(sprintf('%s(%s) is deprecated since Symfony 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryClass), E_USER_DEPRECATED);
 102  
 103          $this->factoryClass = $factoryClass;
 104  
 105          return $this;
 106      }
 107  
 108      /**
 109       * Gets the factory class.
 110       *
 111       * @return string|null The factory class name
 112       *
 113       * @deprecated since version 2.6, to be removed in 3.0.
 114       */
 115      public function getFactoryClass($triggerDeprecationError = true)
 116      {
 117          if ($triggerDeprecationError) {
 118              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
 119          }
 120  
 121          return $this->factoryClass;
 122      }
 123  
 124      /**
 125       * Sets the factory method able to create an instance of this class.
 126       *
 127       * @param string $factoryMethod The factory method name
 128       *
 129       * @return $this
 130       *
 131       * @deprecated since version 2.6, to be removed in 3.0.
 132       */
 133      public function setFactoryMethod($factoryMethod)
 134      {
 135          @trigger_error(sprintf('%s(%s) is deprecated since Symfony 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryMethod), E_USER_DEPRECATED);
 136  
 137          $this->factoryMethod = $factoryMethod;
 138  
 139          return $this;
 140      }
 141  
 142      /**
 143       * Sets the service that this service is decorating.
 144       *
 145       * @param string|null $id        The decorated service id, use null to remove decoration
 146       * @param string|null $renamedId The new decorated service id
 147       * @param int         $priority  The priority of decoration
 148       *
 149       * @return $this
 150       *
 151       * @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
 152       */
 153      public function setDecoratedService($id, $renamedId = null, $priority = 0)
 154      {
 155          if ($renamedId && $id === $renamedId) {
 156              throw new \InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
 157          }
 158  
 159          if (null === $id) {
 160              $this->decoratedService = null;
 161          } else {
 162              $this->decoratedService = array($id, $renamedId, (int) $priority);
 163          }
 164  
 165          return $this;
 166      }
 167  
 168      /**
 169       * Gets the service that this service is decorating.
 170       *
 171       * @return array|null An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
 172       */
 173      public function getDecoratedService()
 174      {
 175          return $this->decoratedService;
 176      }
 177  
 178      /**
 179       * Gets the factory method.
 180       *
 181       * @return string|null The factory method name
 182       *
 183       * @deprecated since version 2.6, to be removed in 3.0.
 184       */
 185      public function getFactoryMethod($triggerDeprecationError = true)
 186      {
 187          if ($triggerDeprecationError) {
 188              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
 189          }
 190  
 191          return $this->factoryMethod;
 192      }
 193  
 194      /**
 195       * Sets the name of the service that acts as a factory using the factory method.
 196       *
 197       * @param string $factoryService The factory service id
 198       *
 199       * @return $this
 200       *
 201       * @deprecated since version 2.6, to be removed in 3.0.
 202       */
 203      public function setFactoryService($factoryService, $triggerDeprecationError = true)
 204      {
 205          if ($triggerDeprecationError) {
 206              @trigger_error(sprintf('%s(%s) is deprecated since Symfony 2.6 and will be removed in 3.0. Use Definition::setFactory() instead.', __METHOD__, $factoryService), E_USER_DEPRECATED);
 207          }
 208  
 209          $this->factoryService = $factoryService;
 210  
 211          return $this;
 212      }
 213  
 214      /**
 215       * Gets the factory service id.
 216       *
 217       * @return string|null The factory service id
 218       *
 219       * @deprecated since version 2.6, to be removed in 3.0.
 220       */
 221      public function getFactoryService($triggerDeprecationError = true)
 222      {
 223          if ($triggerDeprecationError) {
 224              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
 225          }
 226  
 227          return $this->factoryService;
 228      }
 229  
 230      /**
 231       * Sets the service class.
 232       *
 233       * @param string $class The service class
 234       *
 235       * @return $this
 236       */
 237      public function setClass($class)
 238      {
 239          $this->class = $class;
 240  
 241          return $this;
 242      }
 243  
 244      /**
 245       * Gets the service class.
 246       *
 247       * @return string|null The service class
 248       */
 249      public function getClass()
 250      {
 251          return $this->class;
 252      }
 253  
 254      /**
 255       * Sets the arguments to pass to the service constructor/factory method.
 256       *
 257       * @return $this
 258       */
 259      public function setArguments(array $arguments)
 260      {
 261          $this->arguments = $arguments;
 262  
 263          return $this;
 264      }
 265  
 266      /**
 267       * Sets the properties to define when creating the service.
 268       *
 269       * @return $this
 270       */
 271      public function setProperties(array $properties)
 272      {
 273          $this->properties = $properties;
 274  
 275          return $this;
 276      }
 277  
 278      /**
 279       * Gets the properties to define when creating the service.
 280       *
 281       * @return array
 282       */
 283      public function getProperties()
 284      {
 285          return $this->properties;
 286      }
 287  
 288      /**
 289       * Sets a specific property.
 290       *
 291       * @param string $name
 292       * @param mixed  $value
 293       *
 294       * @return $this
 295       */
 296      public function setProperty($name, $value)
 297      {
 298          $this->properties[$name] = $value;
 299  
 300          return $this;
 301      }
 302  
 303      /**
 304       * Adds an argument to pass to the service constructor/factory method.
 305       *
 306       * @param mixed $argument An argument
 307       *
 308       * @return $this
 309       */
 310      public function addArgument($argument)
 311      {
 312          $this->arguments[] = $argument;
 313  
 314          return $this;
 315      }
 316  
 317      /**
 318       * Replaces a specific argument.
 319       *
 320       * @param int   $index
 321       * @param mixed $argument
 322       *
 323       * @return $this
 324       *
 325       * @throws OutOfBoundsException When the replaced argument does not exist
 326       */
 327      public function replaceArgument($index, $argument)
 328      {
 329          if (0 === \count($this->arguments)) {
 330              throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
 331          }
 332  
 333          if ($index < 0 || $index > \count($this->arguments) - 1) {
 334              throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, \count($this->arguments) - 1));
 335          }
 336  
 337          $this->arguments[$index] = $argument;
 338  
 339          return $this;
 340      }
 341  
 342      /**
 343       * Gets the arguments to pass to the service constructor/factory method.
 344       *
 345       * @return array The array of arguments
 346       */
 347      public function getArguments()
 348      {
 349          return $this->arguments;
 350      }
 351  
 352      /**
 353       * Gets an argument to pass to the service constructor/factory method.
 354       *
 355       * @param int $index
 356       *
 357       * @return mixed The argument value
 358       *
 359       * @throws OutOfBoundsException When the argument does not exist
 360       */
 361      public function getArgument($index)
 362      {
 363          if ($index < 0 || $index > \count($this->arguments) - 1) {
 364              throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, \count($this->arguments) - 1));
 365          }
 366  
 367          return $this->arguments[$index];
 368      }
 369  
 370      /**
 371       * Sets the methods to call after service initialization.
 372       *
 373       * @return $this
 374       */
 375      public function setMethodCalls(array $calls = array())
 376      {
 377          $this->calls = array();
 378          foreach ($calls as $call) {
 379              $this->addMethodCall($call[0], $call[1]);
 380          }
 381  
 382          return $this;
 383      }
 384  
 385      /**
 386       * Adds a method to call after service initialization.
 387       *
 388       * @param string $method    The method name to call
 389       * @param array  $arguments An array of arguments to pass to the method call
 390       *
 391       * @return $this
 392       *
 393       * @throws InvalidArgumentException on empty $method param
 394       */
 395      public function addMethodCall($method, array $arguments = array())
 396      {
 397          if (empty($method)) {
 398              throw new InvalidArgumentException('Method name cannot be empty.');
 399          }
 400          $this->calls[] = array($method, $arguments);
 401  
 402          return $this;
 403      }
 404  
 405      /**
 406       * Removes a method to call after service initialization.
 407       *
 408       * @param string $method The method name to remove
 409       *
 410       * @return $this
 411       */
 412      public function removeMethodCall($method)
 413      {
 414          foreach ($this->calls as $i => $call) {
 415              if ($call[0] === $method) {
 416                  unset($this->calls[$i]);
 417                  break;
 418              }
 419          }
 420  
 421          return $this;
 422      }
 423  
 424      /**
 425       * Check if the current definition has a given method to call after service initialization.
 426       *
 427       * @param string $method The method name to search for
 428       *
 429       * @return bool
 430       */
 431      public function hasMethodCall($method)
 432      {
 433          foreach ($this->calls as $call) {
 434              if ($call[0] === $method) {
 435                  return true;
 436              }
 437          }
 438  
 439          return false;
 440      }
 441  
 442      /**
 443       * Gets the methods to call after service initialization.
 444       *
 445       * @return array An array of method calls
 446       */
 447      public function getMethodCalls()
 448      {
 449          return $this->calls;
 450      }
 451  
 452      /**
 453       * Sets tags for this definition.
 454       *
 455       * @return $this
 456       */
 457      public function setTags(array $tags)
 458      {
 459          $this->tags = $tags;
 460  
 461          return $this;
 462      }
 463  
 464      /**
 465       * Returns all tags.
 466       *
 467       * @return array An array of tags
 468       */
 469      public function getTags()
 470      {
 471          return $this->tags;
 472      }
 473  
 474      /**
 475       * Gets a tag by name.
 476       *
 477       * @param string $name The tag name
 478       *
 479       * @return array An array of attributes
 480       */
 481      public function getTag($name)
 482      {
 483          return isset($this->tags[$name]) ? $this->tags[$name] : array();
 484      }
 485  
 486      /**
 487       * Adds a tag for this definition.
 488       *
 489       * @param string $name       The tag name
 490       * @param array  $attributes An array of attributes
 491       *
 492       * @return $this
 493       */
 494      public function addTag($name, array $attributes = array())
 495      {
 496          $this->tags[$name][] = $attributes;
 497  
 498          return $this;
 499      }
 500  
 501      /**
 502       * Whether this definition has a tag with the given name.
 503       *
 504       * @param string $name
 505       *
 506       * @return bool
 507       */
 508      public function hasTag($name)
 509      {
 510          return isset($this->tags[$name]);
 511      }
 512  
 513      /**
 514       * Clears all tags for a given name.
 515       *
 516       * @param string $name The tag name
 517       *
 518       * @return $this
 519       */
 520      public function clearTag($name)
 521      {
 522          unset($this->tags[$name]);
 523  
 524          return $this;
 525      }
 526  
 527      /**
 528       * Clears the tags for this definition.
 529       *
 530       * @return $this
 531       */
 532      public function clearTags()
 533      {
 534          $this->tags = array();
 535  
 536          return $this;
 537      }
 538  
 539      /**
 540       * Sets a file to require before creating the service.
 541       *
 542       * @param string $file A full pathname to include
 543       *
 544       * @return $this
 545       */
 546      public function setFile($file)
 547      {
 548          $this->file = $file;
 549  
 550          return $this;
 551      }
 552  
 553      /**
 554       * Gets the file to require before creating the service.
 555       *
 556       * @return string|null The full pathname to include
 557       */
 558      public function getFile()
 559      {
 560          return $this->file;
 561      }
 562  
 563      /**
 564       * Sets if the service must be shared or not.
 565       *
 566       * @param bool $shared Whether the service must be shared or not
 567       *
 568       * @return $this
 569       */
 570      public function setShared($shared)
 571      {
 572          $this->shared = (bool) $shared;
 573  
 574          return $this;
 575      }
 576  
 577      /**
 578       * Whether this service is shared.
 579       *
 580       * @return bool
 581       */
 582      public function isShared()
 583      {
 584          return $this->shared;
 585      }
 586  
 587      /**
 588       * Sets the scope of the service.
 589       *
 590       * @param string $scope Whether the service must be shared or not
 591       *
 592       * @return $this
 593       *
 594       * @deprecated since version 2.8, to be removed in 3.0.
 595       */
 596      public function setScope($scope, $triggerDeprecationError = true)
 597      {
 598          if ($triggerDeprecationError) {
 599              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
 600          }
 601  
 602          if (ContainerInterface::SCOPE_PROTOTYPE === $scope) {
 603              $this->setShared(false);
 604          }
 605  
 606          $this->scope = $scope;
 607  
 608          return $this;
 609      }
 610  
 611      /**
 612       * Returns the scope of the service.
 613       *
 614       * @return string
 615       *
 616       * @deprecated since version 2.8, to be removed in 3.0.
 617       */
 618      public function getScope($triggerDeprecationError = true)
 619      {
 620          if ($triggerDeprecationError) {
 621              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
 622          }
 623  
 624          return $this->scope;
 625      }
 626  
 627      /**
 628       * Sets the visibility of this service.
 629       *
 630       * @param bool $boolean
 631       *
 632       * @return $this
 633       */
 634      public function setPublic($boolean)
 635      {
 636          $this->public = (bool) $boolean;
 637  
 638          return $this;
 639      }
 640  
 641      /**
 642       * Whether this service is public facing.
 643       *
 644       * @return bool
 645       */
 646      public function isPublic()
 647      {
 648          return $this->public;
 649      }
 650  
 651      /**
 652       * Sets the synchronized flag of this service.
 653       *
 654       * @param bool $boolean
 655       *
 656       * @return $this
 657       *
 658       * @deprecated since version 2.7, will be removed in 3.0.
 659       */
 660      public function setSynchronized($boolean, $triggerDeprecationError = true)
 661      {
 662          if ($triggerDeprecationError) {
 663              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
 664          }
 665  
 666          $this->synchronized = (bool) $boolean;
 667  
 668          return $this;
 669      }
 670  
 671      /**
 672       * Whether this service is synchronized.
 673       *
 674       * @return bool
 675       *
 676       * @deprecated since version 2.7, will be removed in 3.0.
 677       */
 678      public function isSynchronized($triggerDeprecationError = true)
 679      {
 680          if ($triggerDeprecationError) {
 681              @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
 682          }
 683  
 684          return $this->synchronized;
 685      }
 686  
 687      /**
 688       * Sets the lazy flag of this service.
 689       *
 690       * @param bool $lazy
 691       *
 692       * @return $this
 693       */
 694      public function setLazy($lazy)
 695      {
 696          $this->lazy = (bool) $lazy;
 697  
 698          return $this;
 699      }
 700  
 701      /**
 702       * Whether this service is lazy.
 703       *
 704       * @return bool
 705       */
 706      public function isLazy()
 707      {
 708          return $this->lazy;
 709      }
 710  
 711      /**
 712       * Sets whether this definition is synthetic, that is not constructed by the
 713       * container, but dynamically injected.
 714       *
 715       * @param bool $boolean
 716       *
 717       * @return $this
 718       */
 719      public function setSynthetic($boolean)
 720      {
 721          $this->synthetic = (bool) $boolean;
 722  
 723          return $this;
 724      }
 725  
 726      /**
 727       * Whether this definition is synthetic, that is not constructed by the
 728       * container, but dynamically injected.
 729       *
 730       * @return bool
 731       */
 732      public function isSynthetic()
 733      {
 734          return $this->synthetic;
 735      }
 736  
 737      /**
 738       * Whether this definition is abstract, that means it merely serves as a
 739       * template for other definitions.
 740       *
 741       * @param bool $boolean
 742       *
 743       * @return $this
 744       */
 745      public function setAbstract($boolean)
 746      {
 747          $this->abstract = (bool) $boolean;
 748  
 749          return $this;
 750      }
 751  
 752      /**
 753       * Whether this definition is abstract, that means it merely serves as a
 754       * template for other definitions.
 755       *
 756       * @return bool
 757       */
 758      public function isAbstract()
 759      {
 760          return $this->abstract;
 761      }
 762  
 763      /**
 764       * Whether this definition is deprecated, that means it should not be called
 765       * anymore.
 766       *
 767       * @param bool   $status
 768       * @param string $template Template message to use if the definition is deprecated
 769       *
 770       * @return $this
 771       *
 772       * @throws InvalidArgumentException when the message template is invalid
 773       */
 774      public function setDeprecated($status = true, $template = null)
 775      {
 776          if (null !== $template) {
 777              if (preg_match('#[\r\n]|\*/#', $template)) {
 778                  throw new InvalidArgumentException('Invalid characters found in deprecation template.');
 779              }
 780  
 781              if (false === strpos($template, '%service_id%')) {
 782                  throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
 783              }
 784  
 785              $this->deprecationTemplate = $template;
 786          }
 787  
 788          $this->deprecated = (bool) $status;
 789  
 790          return $this;
 791      }
 792  
 793      /**
 794       * Whether this definition is deprecated, that means it should not be called
 795       * anymore.
 796       *
 797       * @return bool
 798       */
 799      public function isDeprecated()
 800      {
 801          return $this->deprecated;
 802      }
 803  
 804      /**
 805       * Message to use if this definition is deprecated.
 806       *
 807       * @param string $id Service id relying on this definition
 808       *
 809       * @return string
 810       */
 811      public function getDeprecationMessage($id)
 812      {
 813          return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
 814      }
 815  
 816      /**
 817       * Sets a configurator to call after the service is fully initialized.
 818       *
 819       * @param callable $callable A PHP callable
 820       *
 821       * @return $this
 822       */
 823      public function setConfigurator($callable)
 824      {
 825          $this->configurator = $callable;
 826  
 827          return $this;
 828      }
 829  
 830      /**
 831       * Gets the configurator to call after the service is fully initialized.
 832       *
 833       * @return callable|null The PHP callable to call
 834       */
 835      public function getConfigurator()
 836      {
 837          return $this->configurator;
 838      }
 839  
 840      /**
 841       * Sets types that will default to this definition.
 842       *
 843       * @param string[] $types
 844       *
 845       * @return $this
 846       */
 847      public function setAutowiringTypes(array $types)
 848      {
 849          $this->autowiringTypes = array();
 850  
 851          foreach ($types as $type) {
 852              $this->autowiringTypes[$type] = true;
 853          }
 854  
 855          return $this;
 856      }
 857  
 858      /**
 859       * Is the definition autowired?
 860       *
 861       * @return bool
 862       */
 863      public function isAutowired()
 864      {
 865          return $this->autowired;
 866      }
 867  
 868      /**
 869       * Enables/disables autowiring.
 870       *
 871       * @param bool $autowired
 872       *
 873       * @return $this
 874       */
 875      public function setAutowired($autowired)
 876      {
 877          $this->autowired = $autowired;
 878  
 879          return $this;
 880      }
 881  
 882      /**
 883       * Gets autowiring types that will default to this definition.
 884       *
 885       * @return string[]
 886       */
 887      public function getAutowiringTypes()
 888      {
 889          return array_keys($this->autowiringTypes);
 890      }
 891  
 892      /**
 893       * Adds a type that will default to this definition.
 894       *
 895       * @param string $type
 896       *
 897       * @return $this
 898       */
 899      public function addAutowiringType($type)
 900      {
 901          $this->autowiringTypes[$type] = true;
 902  
 903          return $this;
 904      }
 905  
 906      /**
 907       * Removes a type.
 908       *
 909       * @param string $type
 910       *
 911       * @return $this
 912       */
 913      public function removeAutowiringType($type)
 914      {
 915          unset($this->autowiringTypes[$type]);
 916  
 917          return $this;
 918      }
 919  
 920      /**
 921       * Will this definition default for the given type?
 922       *
 923       * @param string $type
 924       *
 925       * @return bool
 926       */
 927      public function hasAutowiringType($type)
 928      {
 929          return isset($this->autowiringTypes[$type]);
 930      }
 931  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1