public function testProxyClass() { $config = array(array('class' => 'TestServiceForProxy', 'id' => 'TestServiceForProxy'), array('class' => 'TestAspectBean', 'id' => 'PreCallTest'), array('class' => 'TestAspectBean', 'id' => 'PostCallTest'), array('src' => dirname(dirname(dirname(__FILE__))) . '/services/AopProxyService.php', 'id' => 'TestService', 'properties' => array('proxied' => '#$TestServiceForProxy', 'beforeCall' => array('calculate' => '#$PreCallTest'), 'afterCall' => array('calculate' => '#$PostCallTest')))); $injector = new Injector($config); $test = $injector->get('TestService'); $this->assertEqual('AopProxyService', get_class($test)); $this->assertEqual(10, $test->calculate(5)); $aspect1 = $injector->get('PreCallTest'); $this->assertEqual(5, $aspect1->data['calculatepre']); $aspect2 = $injector->get('PostCallTest'); $this->assertEqual(10, $aspect2->data['calculatepost']); $this->assertNotEqual($aspect1, $aspect2); }
/** * Unserialize an object * @param string $string * @return mixed */ public static function unserialize($string) { $unserialzed = unserialize($string); if ($unserialzed instanceof InjectOnWakeup) { Injector::get()->inject($unserialzed); } return $unserialzed; }
/** * @expectedException InvalidArgumentException */ public function testTooManyArrayValues() { $injector = new Injector(); $config = array('TestService' => array('class' => 'TestObject', 'calls' => array(array('method', array('args'), 'what is this?')))); $injector->load($config); $item = $injector->get('TestService'); }
public function testSameNamedSingeltonPrototype() { $injector = new Injector(); // get a singleton object $object = $injector->get('NeedsBothCirculars'); $object->var = 'One'; $again = $injector->get('NeedsBothCirculars'); $this->assertEquals($again->var, 'One'); // create a NEW instance object $new = $injector->create('NeedsBothCirculars'); $this->assertNull($new->var); // this will trigger a problem below $new->var = 'Two'; $again = $injector->get('NeedsBothCirculars'); $this->assertEquals($again->var, 'One'); }
public function testInheritedConfig() { $injector = new Injector(array('locator' => 'SilverStripeServiceConfigurationLocator')); Config::inst()->update('Injector', 'MyParentClass', array('properties' => array('one' => 'the one'))); $obj = $injector->get('MyParentClass'); $this->assertEquals($obj->one, 'the one'); $obj = $injector->get('MyChildClass'); $this->assertEquals($obj->one, 'the one'); }
/** * Tests creating a service with a custom factory. */ public function testCustomFactory() { $injector = new Injector(array('service' => array('factory' => 'factory', 'constructor' => array(1, 2, 3)))); $factory = $this->getMock('SilverStripe\\Framework\\Injector\\Factory'); $factory->expects($this->once())->method('create')->with($this->equalTo('service'), $this->equalTo(array(1, 2, 3)))->will($this->returnCallback(function ($args) { return new TestObject(); })); $injector->registerService($factory, 'factory'); $this->assertInstanceOf('TestObject', $injector->get('service')); }
public function onInstantiate(Component &$component) { Injector::get()->inject($component); }
/** * Calls to webservices are routed through here and converted * from url params to method calls. * * @return mixed */ public function handleService() { $service = ucfirst($this->request->param('Service')) . 'Service'; $method = $this->request->param('Method'); $body = $this->request->getBody(); $requestType = strlen($body) > 0 ? 'POST' : $this->request->httpMethod(); // (count($this->request->postVars()) > 0 ? 'POST' : 'GET'); $svc = $this->injector->get($service); $response = ''; if ($svc && ($svc instanceof WebServiceable || method_exists($svc, 'webEnabledMethods'))) { $allowedMethods = array(); if (method_exists($svc, 'webEnabledMethods')) { $allowedMethods = $svc->webEnabledMethods(); } // if we have a list of methods, lets use those to restrict if (count($allowedMethods)) { $this->checkMethods($method, $allowedMethods, $requestType); } else { // we only allow 'read only' requests so we wrap everything // in a readonly transaction so that any database request // disallows write() calls // @TODO } if (!Member::currentUserID()) { // require service to explicitly state that the method is allowed if (method_exists($svc, 'publicWebMethods')) { $publicMethods = $svc->publicWebMethods(); if (!isset($publicMethods[$method])) { throw new WebServiceException(403, "Public method {$method} not allowed"); } } else { throw new WebServiceException(403, "Method {$method} not allowed; no public methods defined"); } } $refObj = new ReflectionObject($svc); $refMeth = $refObj->getMethod($method); /* @var $refMeth ReflectionMethod */ if ($refMeth) { $allArgs = $this->getRequestArgs($requestType); $refParams = $refMeth->getParameters(); $params = array(); foreach ($refParams as $refParm) { /* @var $refParm ReflectionParameter */ $paramClass = $refParm->getClass(); // if we're after a dataobject, we'll try and find one using // this name with ID and Type parameters if ($paramClass && ($paramClass->getName() == 'DataObject' || $paramClass->isSubclassOf('DataObject'))) { $idArg = $refParm->getName() . 'ID'; $typeArg = $refParm->getName() . 'Type'; if (isset($allArgs[$idArg]) && isset($allArgs[$typeArg]) && class_exists($allArgs[$typeArg])) { $object = null; if (class_exists('DataService')) { $object = $this->injector->DataService->byId($allArgs[$typeArg], $allArgs[$idArg]); } else { $object = DataObject::get_by_id($allArgs[$typeArg], $allArgs[$idArg]); if (!$object->canView()) { $object = null; } } if ($object) { $params[$refParm->getName()] = $object; } } else { $params[$refParm->getName()] = null; } } else { if (isset($allArgs[$refParm->getName()])) { $params[$refParm->getName()] = $allArgs[$refParm->getName()]; } else { if ($refParm->getName() == 'file' && $requestType == 'POST') { // special case of a binary file upload $params['file'] = $body; } else { if ($refParm->isOptional()) { $params[$refParm->getName()] = $refParm->getDefaultValue(); } else { throw new WebServiceException(500, "Service method {$method} expects parameter " . $refParm->getName()); } } } } } $return = $refMeth->invokeArgs($svc, $params); $responseItem = $this->convertResponse($return); $response = $this->converters[$this->format]['FinalConverter']->convert($responseItem); } } $this->response->setBody($response); return $this->response; }
public function __construct() { Injector::get()->inject($this); }
/** * Send this HTTPReponse to the browser */ public function output() { // Attach appropriate X-Include-JavaScript and X-Include-CSS headers if (Director::is_ajax()) { Requirements::include_in_response($this); } if (in_array($this->statusCode, self::$redirect_codes) && headers_sent($file, $line)) { $url = Director::absoluteURL($this->headers['Location'], true); $urlATT = Convert::raw2htmlatt($url); $urlJS = Convert::raw2js($url); $title = Director::isDev() ? "{$urlATT}... (output started on {$file}, line {$line})" : "{$urlATT}..."; echo <<<EOT <p>Redirecting to <a href="{$urlATT}" title="Click this link if your browser does not redirect you">{$title}</a></p> <meta http-equiv="refresh" content="1; url={$urlATT}" /> <script type="text/javascript">setTimeout(function(){ \twindow.location.href = "{$urlJS}"; }, 50);</script>"; EOT; } else { $line = $file = null; if (!headers_sent($file, $line)) { header($_SERVER['SERVER_PROTOCOL'] . " {$this->statusCode} " . $this->getStatusDescription()); foreach ($this->headers as $header => $value) { header("{$header}: {$value}", true, $this->statusCode); } } else { // It's critical that these status codes are sent; we need to report a failure if not. if ($this->statusCode >= 300) { user_error("Couldn't set response type to {$this->statusCode} because " . "of output on line {$line} of {$file}", E_USER_WARNING); } } // Only show error pages or generic "friendly" errors if the status code signifies // an error, and the response doesn't have any body yet that might contain // a more specific error description. if (Director::isLive() && $this->isError() && !$this->body) { $formatter = Injector::get('FriendlyErrorFormatter'); $formatter->setStatusCode($this->statusCode); echo $formatter->format(array()); } else { echo $this->body; } } }
/** * Resolve the given job handler. * * @param string $class * * @return mixed */ protected function resolve($class) { return $this->injector->get($class); }