Api\V1\Engine\Api::initialize PHP Method

initialize() public method

This method exists because the service container needs to be set before the rest of API functionality gets loaded.
public initialize ( )
    public function initialize()
    {
        self::$content = null;
        /**
         * @var Request
         */
        $request = $this->getContainer()->get('request');
        // simulate $_REQUEST
        $parameters = array_merge((array) $request->query->all(), (array) $request->request->all());
        $method = $request->get('method');
        if ($method == '') {
            return self::output(self::BAD_REQUEST, array('message' => 'No method-parameter provided.'));
        }
        // process method
        $chunks = (array) explode('.', $method, 2);
        // validate method
        if (!isset($chunks[1])) {
            return self::output(self::BAD_REQUEST, array('message' => 'Invalid method.'));
        }
        // camelcase module name
        $chunks[0] = \SpoonFilter::toCamelCase($chunks[0]);
        // build the path to the backend API file
        if ($chunks[0] == 'Core') {
            $class = 'Backend\\Core\\Engine\\Api';
        } else {
            $class = 'Backend\\Modules\\' . $chunks[0] . '\\Engine\\Api';
        }
        // check if the file is present? If it isn't present there is a problem
        if (!class_exists($class)) {
            return self::output(self::BAD_REQUEST, array('message' => 'Invalid method.'));
        }
        // build config-object-name
        $methodName = \SpoonFilter::toCamelCase($chunks[1], '.', true);
        // validate if the method exists
        if (!is_callable(array($class, $methodName))) {
            return self::output(self::BAD_REQUEST, array('message' => 'Invalid method.'));
        }
        // call the method
        try {
            // init var
            $arguments = null;
            // create reflection method
            $reflectionMethod = new \ReflectionMethod($class, $methodName);
            $parameterDocumentation = array();
            // get data from docs
            $matches = array();
            preg_match_all('/@param[\\s\\t]+(.*)[\\s\\t]+\\$(.*)[\\s\\t]+(.*)$/Um', $reflectionMethod->getDocComment(), $matches);
            // documentation found
            if (!empty($matches[0])) {
                // loop matches
                foreach ($matches[0] as $i => $row) {
                    // set documentation
                    $parameterDocumentation[$matches[2][$i]] = array('type' => $matches[1][$i], 'optional' => mb_substr_count($matches[1][$i], '[optional]') > 0, 'description' => $matches[3][$i]);
                }
            }
            // loop parameters
            foreach ($reflectionMethod->getParameters() as $parameter) {
                // init var
                $name = $parameter->getName();
                // check if the parameter is available
                if (!$parameter->isOptional() && !isset($parameters[$name])) {
                    return self::output(self::BAD_REQUEST, array('message' => 'No ' . $name . '-parameter provided.'));
                }
                // add not-passed arguments
                if ($parameter->isOptional() && !isset($parameters[$name])) {
                    $arguments[] = $parameter->getDefaultValue();
                } elseif (isset($parameterDocumentation[$name]['type'])) {
                    // add argument if we know the type
                    // get default value
                    $defaultValue = null;
                    if ($parameter->isOptional()) {
                        $defaultValue = $parameter->getDefaultValue();
                    }
                    // add argument
                    $arguments[] = \SpoonFilter::getValue($parameters[$name], null, $defaultValue, $parameterDocumentation[$name]['type']);
                } else {
                    // fallback
                    $arguments[] = $parameters[$name];
                }
            }
            // get the return
            $data = (array) call_user_func_array(array($class, $methodName), (array) $arguments);
            // output
            if (self::$content === null) {
                self::output(self::OK, $data);
            }
        } catch (\Exception $e) {
            // if we are debugging we should see the exceptions
            if ($this->getContainer()->getParameter('kernel.debug') && (!isset($parameters['debug']) || $parameters['debug'] === 'true')) {
                throw $e;
            }
            // output
            return self::output(500, array('message' => $e->getMessage()));
        }
    }