/** * Helper method to find and execute a command. Magic method calls must be * enabled on the client to use this functionality. * * @param string $method Name of the command object to instantiate * @param array $args Arguments to pass to the command * * @return mixed * @throws BadMethodCallException when a command is not found or magic * methods are disabled */ public function __call($method, $args = null) { if ($this->magicMethodBehavior == self::MAGIC_CALL_DISABLED) { throw new BadMethodCallException("Missing method {$method}. Enable magic calls to use magic methods with command names."); } $command = $this->getCommand(Inflector::snake($method), $args); return $this->magicMethodBehavior == self::MAGIC_CALL_RETURN ? $command : $this->execute($command); }
/** * Create a resource iterator * * @param CommandInterface $data Command used for building the iterator * @param array $options Iterator options. * * @return ResourceIteratorInterface */ public function build($data, array $options = null) { if (!$data instanceof CommandInterface) { throw new InvalidArgumentException('The first argument must be an ' . 'instance of CommandInterface'); } // Determine the name of the class to load $className = $this->baseNamespace . '\\' . Inflector::camel($data->getName()) . 'Iterator'; return new $className($data, $options); }
/** * {@inheritdoc} */ public function factory($name, array $args = array()) { // Determine the class to instantiate based on the namespace of the // current client and the default location of commands $prefix = $this->client->getConfig('command.prefix'); if (!$prefix) { // The prefix can be specified in a factory method and is cached $prefix = implode('\\', array_slice(explode('\\', get_class($this->client)), 0, -1)) . '\\Command\\'; $this->client->getConfig()->set('command.prefix', $prefix); } $class = $prefix . str_replace(' ', '\\', ucwords(str_replace('.', ' ', Inflector::camel($name)))); // Create the concrete command if it exists if (class_exists($class)) { return new $class($args); } }
/** * Enables magic methods for setting parameters. * * @param string $method Name of the parameter to set * @param array $args (optional) Arguments to pass to the command * * @return AbstractCommand * @throws BadMethodCallException when a parameter doesn't exist */ public function __call($method, $args = null) { // Ensure magic method call behavior is enabled if (!$this->get('command.magic_method_call')) { throw new BadMethodCallException('Magic method calls are disabled ' . 'for this command. Consider enabling magic method calls by ' . 'setting the command.magic_method_call parameter to true.'); } if ($args && strpos($method, 'set') === 0) { // Convert the method into the snake cased parameter key $key = Inflector::snake(substr($method, 3)); // If the parameter exists, set it if (array_key_exists($key, $this->apiCommand->getParams())) { $this->set($key, $args[0]); return $this; } } // If the method is not a set method, or the parameter doesn't exist, fail throw new BadMethodCallException("Missing method {$method}."); }