MediaWiki REL1_28
TestingAccessWrapper.php
Go to the documentation of this file.
1<?php
19 public $object;
20
24 public static function newFromObject( $object ) {
25 if ( !is_object( $object ) ) {
26 throw new InvalidArgumentException( __METHOD__ . ' must be called with an object' );
27 }
28 $wrapper = new TestingAccessWrapper();
29 $wrapper->object = $object;
30 return $wrapper;
31 }
32
37 public static function newFromClass( $className ) {
38 if ( !is_string( $className ) ) {
39 throw new InvalidArgumentException( __METHOD__ . ' must be called with a class name' );
40 }
41 $wrapper = new TestingAccessWrapper();
42 $wrapper->object = $className;
43 return $wrapper;
44 }
45
46 public function __call( $method, $args ) {
47 $methodReflection = $this->getMethod( $method );
48
49 if ( $this->isStatic() && !$methodReflection->isStatic() ) {
50 throw new DomainException( __METHOD__ . ': Cannot call non-static when wrapping static class' );
51 }
52
53 return $methodReflection->invokeArgs( $methodReflection->isStatic() ? null : $this->object,
54 $args );
55 }
56
57 public function __set( $name, $value ) {
58 $propertyReflection = $this->getProperty( $name );
59
60 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
61 throw new DomainException( __METHOD__ . ': Cannot set property when wrapping static class' );
62 }
63
64 $propertyReflection->setValue( $this->object, $value );
65 }
66
67 public function __get( $name ) {
68 $propertyReflection = $this->getProperty( $name );
69
70 if ( $this->isStatic() && !$propertyReflection->isStatic() ) {
71 throw new DomainException( __METHOD__ . ': Cannot get property when wrapping static class' );
72 }
73
74 return $propertyReflection->getValue( $this->object );
75 }
76
77 private function isStatic() {
78 return is_string( $this->object );
79 }
80
86 private function getMethod( $name ) {
87 $classReflection = new ReflectionClass( $this->object );
88 $methodReflection = $classReflection->getMethod( $name );
89 $methodReflection->setAccessible( true );
90 return $methodReflection;
91 }
92
103 private function getProperty( $name ) {
104 $classReflection = new ReflectionClass( $this->object );
105 try {
106 $propertyReflection = $classReflection->getProperty( $name );
107 } catch ( ReflectionException $ex ) {
108 while ( true ) {
109 $classReflection = $classReflection->getParentClass();
110 if ( !$classReflection ) {
111 throw $ex;
112 }
113 try {
114 $propertyReflection = $classReflection->getProperty( $name );
115 } catch ( ReflectionException $ex2 ) {
116 continue;
117 }
118 if ( $propertyReflection->isPrivate() ) {
119 break;
120 } else {
121 throw $ex;
122 }
123 }
124 }
125 $propertyReflection->setAccessible( true );
126 return $propertyReflection;
127 }
128}
if( $line===false) $args
Definition cdb.php:64
Circumvent access restrictions on object internals.
getProperty( $name)
Return a property and make it accessible.
static newFromClass( $className)
Allow access to non-public static methods and properties of the class.
mixed $object
The object, or the class name for static-only access.
static newFromObject( $object)
Return the same object, without access restrictions.
getMethod( $name)
Return a property and make it accessible.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37