/**
  * Constructor.
  * 
  * @param string $expected Expected type.
  * @param mixed $actual Actual argument given.
  * @param int $number [optional] Argument number.
  * @param bool $hideCaller [optional] Should the function that has thrown this exception be hidden? Default: `false`.
  */
 public function __construct($expected, $actual, $number = 1, $hideCaller = false)
 {
     $trace = Debugger::getPrettyTrace(debug_backtrace());
     $type = Debugger::getType($actual);
     $type = $type === 'string' ? $type . ' ("' . StringUtils::truncate($actual, 50) . '")' : $type;
     if (!$hideCaller && isset($trace[1])) {
         $message = $trace[1]['function'] . ' expected argument ' . $number . ' to be ' . $expected . ', ' . $type . ' given.';
     } else {
         $message = 'Expected argument ' . $number . ' to be ' . $expected . ', ' . $type . ' given.';
     }
     parent::__construct($message);
 }
 public function testGetType()
 {
     $this->assertEquals('integer', Debugger::getType(13));
     $this->assertEquals('string', Debugger::getType('13'));
     $this->assertEquals('boolean', Debugger::getType(true));
     $this->assertEquals('boolean', Debugger::getType(false));
     $this->assertEquals('NULL', Debugger::getType(null));
     $this->assertEquals('array', Debugger::getType(array()));
     $this->assertEquals('double', Debugger::getType(0.5));
     $this->assertEquals(get_called_class(), Debugger::getType($this));
     $this->assertEquals('MD\\Foundation\\Tests\\TestFixtures\\Collection', Debugger::getType(new Collection()));
     $this->assertEquals('stdClass', Debugger::getType(new \stdClass()));
     $this->assertEquals('closure', Debugger::getType(function () {
     }));
 }
Exemple #3
0
 /**
  * Handles invoking magic methods `::findBy*` and `::findOneBy*`.
  *
  * @param string $method    Called method name.
  * @param array  $arguments Array of arguments the method was called with.
  *
  * @return array|object|null
  *
  * @throws \InvalidArgumentException If any of the arguments is invalid.
  * @throws \BadMethodCallException When couldn't resolve to a valid method.
  */
 public function __call($method, array $arguments)
 {
     if (!isset($arguments[0])) {
         throw new \InvalidArgumentException(sprintf('Missing 1st argument for method ::%s', $method));
     }
     $params = [];
     if (isset($arguments[1])) {
         if (!is_array($arguments[1])) {
             throw new \InvalidArgumentException(sprintf('2nd argument for method ::%s must be an array, %s given.', $method, Debugger::getType($arguments[1])));
         }
         $params = $arguments[1];
     }
     if (strpos($method, 'findBy') === 0) {
         $property = StringUtils::toSeparated(substr($method, 6), '_');
         return $this->find([$property => $arguments[0]], $params);
     }
     if (strpos($method, 'findOneBy') === 0) {
         $property = StringUtils::toSeparated(substr($method, 9), '_');
         return $this->findOne([$property => $arguments[0]], $params);
     }
     throw new \BadMethodCallException(sprintf('Call to undefined method %s::%s', __CLASS__, $method));
 }
Exemple #4
0
 public function __construct($name, $object)
 {
     parent::__construct($name);
     $this->instance = $object;
     $this->class = Debugger::getType($object);
 }
Exemple #5
0
 /**
  * Set a service by passing an object instance.
  *
  * Also accepts closures which will be treated as factories.
  *
  * @param string  $name      Name of the service.
  * @param object|closure $object Object to be set as a service or a closure that returns the service.
  * @param array   $options   [optional] Array of options for the service definition.
  * @param boolean $singleton Deprecated.
  *
  * @throws ReadOnlyException When trying to overwrite a service that is marked as read only.
  */
 public function set($name, $object, $options = array(), $singleton = true)
 {
     // for backward compatibility
     $options = is_array($options) ? $options : array('read_only' => $options, 'singleton' => $singleton);
     // if overwriting an alias $name then make sure the real service is overwritten, not just the alias
     try {
         $name = $this->resolveServiceName($name);
     } catch (ServiceNotFoundException $e) {
     }
     $service = Debugger::getType($object) === 'closure' ? new ClosureService($name, $object) : new ObjectService($name, $object);
     $this->addService($service, $options);
 }
 /**
  * Execute the given controller.
  * 
  * @param string $name Name of the controller or route assigned to this controller.
  * @param string $class Class name of the controller.
  * @param string $method Method name to execute on the controller.
  * @param array $arguments [optional] Arguments to execute the controller with.
  * @return Response
  */
 protected function renderController($name, $class, $method, array $arguments = array(), Request $request = null)
 {
     $eventManager = $this->container->get('event_manager');
     $controller = new $class($this->container);
     $willRespondEvent = new ControllerWillRespond($name, $controller, $method, $arguments);
     $eventManager->trigger($willRespondEvent);
     $method = $willRespondEvent->getMethod();
     $arguments = $willRespondEvent->getArguments();
     $controllerResponse = new ControllerResponse(call_user_func_array(array($controller, $method), $arguments));
     $eventManager->trigger(new ControllerDidRespond($controllerResponse, $name, $controller, $method, $arguments, $request));
     $response = $controllerResponse->getResponse();
     $this->logger->debug('Executed controller: "{name}"', array('name' => $name, 'function' => $class . '::' . $method, 'arguments' => $arguments, '_timer' => $this->container->get('splot.timer')->step('Matched route')));
     // special case, if the response is a string then automatically convert it to HttpResponse
     if (is_string($response)) {
         $response = new Response($response);
     }
     if (!is_object($response) || !$response instanceof Response) {
         throw new InvalidReturnValueException('Executed controller method must return ' . Response::class . ' instance or a string, "' . Debugger::getType($response) . '" given.');
     }
     return $response;
 }