예제 #1
0
 /**
  *
  */
 public function renderView($viewCode, $viewContext = null, $viewRoot = null)
 {
     // Fix the view root
     if (empty($viewRoot) === true) {
         $viewRoot = PATH_APP . '/Views';
     }
     // Check whether the view exists
     $pathToView = $viewRoot . '/' . $viewCode;
     if (file_exists($pathToView) === false) {
         \z\e(EXCEPTION_VIEW_NOT_FOUND, ['pathToView' => $pathToView, 'viewCode' => $viewCode, 'viewContext' => $viewContext]);
     }
     // Load the view
     $view = file_get_contents($pathToView);
     // Compile the view
     $code = \LightnCandy\LightnCandy::compile($view, ['basedir' => [PATH_APP . '/Views'], 'fileext' => [''], 'flags' => \LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION | \LightnCandy\LightnCandy::FLAG_HANDLEBARS | \LightnCandy\LightnCandy::FLAG_RENDER_DEBUG | \LightnCandy\LightnCandy::FLAG_RUNTIMEPARTIAL | \LightnCandy\LightnCandy::FLAG_THIS, 'helpers' => ['locale' => function () {
         return \z\service('manager/culture')->localeCode;
     }, 'str' => function ($indexed, $associative) {
         // Build arguments
         $arguments = $this->buildArguments(['stringCode'], $indexed, $associative);
         // Get the string
         $result = \z\str($arguments['stringCode']);
         return $result;
     }]]);
     // Build path to code
     $pathToCode = '/tmp/' . sha1('fbenard/zero_' . $pathToView) . '.php';
     // Get the view renderer
     file_put_contents($pathToCode, $code);
     $renderer = (require $pathToCode);
     // Render the view
     $result = $renderer($viewContext);
     return $result;
 }
예제 #2
0
 /**
  *
  */
 public function buildService($serviceCode, $definitions)
 {
     // Make sure the service has a definition
     if (array_key_exists($serviceCode, $definitions) === false) {
         \z\e(EXCEPTION_SERVICE_NOT_FOUND, ['serviceCode' => $serviceCode, 'definitions' => $definitions]);
     }
     // Get the definition
     $definition = $definitions[$serviceCode];
     // If the definition is an object
     // Return it as such
     if (is_object($definition) === true) {
         return $definition;
     }
     // Otherwise, it's a classname
     $className = $definition;
     // Make sure the class exists
     if (class_exists($className) === false) {
         \z\e(EXCEPTION_SERVICE_NOT_FOUND, ['serviceCode' => $serviceCode, 'className' => $className]);
     }
     // Make sure the class is instantiable
     $reflection = new \ReflectionClass($className);
     if ($reflection->isInstantiable() === false) {
         \z\e(EXCEPTION_SERVICE_NOT_INSTANTIABLE, ['serviceCode' => $serviceCode, 'className' => $className]);
     }
     // Create the service
     $service = new $className();
     return $service;
 }
예제 #3
0
파일: HttpHelper.php 프로젝트: fbenard/zero
 /**
  *
  */
 public function call($verb, $host, $port, $uri, $headers = null, $query = null, $body = null, $statusCode = null)
 {
     // Fix headers, query and body
     $headers = \z\conf($headers);
     $query = \z\conf($query);
     if (is_null($body) === true) {
         $body = '';
     }
     // Body must be a string
     if (is_string($body) === false) {
         \z\e(EXCEPTION_HTTP_BODY_NOT_VALID, ['body' => json_encode($body, true), 'type' => gettype($body)]);
     }
     // Build the HTTP client
     $client = new \GuzzleHttp\Client(['base_url' => $host]);
     // Build the request
     $request = $client->createRequest($verb, $uri, ['exceptions' => false]);
     // Setup the request
     $request->setPort($port);
     $request->setHeaders($headers);
     $request->setQuery($query);
     $request->setBody(\GuzzleHttp\Stream\Stream::factory($body));
     // Log
     \z\logger()->debug($request);
     // Send the request
     $response = $client->send($request);
     // Log
     \z\logger()->debug($response);
     // Did it succeed?
     if (is_null($statusCode) === false && (int) $statusCode !== (int) $response->getStatusCode()) {
         \z\e(EXCEPTION_HTTP_STATUS_CODE_NOT_VALID, ['expected' => $statusCode, 'actual' => $response->getStatusCode(), 'request' => $request->__toString(), 'response' => $response->__toString()]);
     }
     // Build the result
     $result = $response->getBody();
     return $result;
 }
예제 #4
0
 /**
  *
  */
 public function getConnection($connectionCode = null)
 {
     // Grab definitions
     $definitions = \z\pref('db/connections');
     if (is_array($definitions) === false) {
         $definitions = [];
     }
     // Define the connection code
     if (empty($connectionCode) === true) {
         $definitions = array_splice($definitions, 0, 1);
         $keys = array_keys($definitions);
         $connectionCode = array_shift($keys);
     }
     // Get the definition
     if (array_key_exists($connectionCode, $definitions) === true) {
         $definition = $definitions[$connectionCode];
     }
     // Check whether there's a definition
     if (empty($definition) === true) {
         \z\e(EXCEPTION_DB_CONNECTION_NOT_FOUND, ['connectionCode' => $connectionCode, 'definitions' => $definitions]);
     }
     // Has the connection been retrieved already?
     if (array_key_exists($connectionCode, $this->_connections) === false) {
         // Build the connection
         $connection = \z\service('factory/connection')->buildConnection($definition);
         // Connect the driver
         $connection->driver->connect($connection);
         // Store the connection
         $this->_connections[$connectionCode] = $connection;
     }
     // Get the connection
     $connection = $this->_connections[$connectionCode];
     return $connection;
 }
예제 #5
0
 /**
  *
  */
 public function check()
 {
     // If the app is not in CLI mode
     // Then permission is not granted
     if (\z\app()->isCli() === false) {
         \z\e(EXCEPTION_PERMISSION_NOT_GRANTED);
     }
 }
예제 #6
0
 /**
  *
  */
 public function __call($methodCode, $methodArguments)
 {
     try {
         // Execute the method
         $result = call_user_func_array([$this->_client, $methodCode], $methodArguments);
         return $result;
     } catch (\Exception $e) {
         \z\e(EXCEPTION_ES_METHOD_FAILED, ['methodCode' => $methodCode, 'methodArguments' => $methodArguments, 'error' => $e->getMessage()]);
     }
 }
예제 #7
0
 /**
  *
  */
 public function executeQuery($query)
 {
     // Execute the query
     $queryResult = $this->_handle->query($query);
     // Check whether the query worked
     if ($queryResult === false) {
         \z\e(EXCEPTION_DB_QUERY_FAILED, ['error' => $this->_handle->error, 'query' => $query]);
     }
     // Fetch all results, if any
     if (is_object($queryResult) === true) {
         return $queryResult->fetch_all(MYSQLI_ASSOC);
     } else {
         return $queryResult;
     }
 }
예제 #8
0
 /**
  *
  */
 public function buildModel($modelCode)
 {
     // Check whether the model exists
     $pathToModel = PATH_APP . '/Config/Models/' . $modelCode . '.json';
     if (file_exists($pathToModel) === false) {
         \z\e(EXCEPTION_MODEL_NOT_FOUND, ['modelCode' => $modelCode, 'pathToModel' => $pathToModel]);
     }
     // Decode the model
     $rawModel = file_get_contents($pathToModel);
     $model = json_decode($rawModel, true);
     // Fix the model
     $model = $this->fixModel($model);
     // Does the model extend a base model?
     if (empty($model['extends']) === false) {
         // Build the base model
         $baseModel = $this->buildModel($model['extends']);
         // Merge properties
         $model['properties'] = array_merge($baseModel['properties'], $model['properties']);
     }
     return $model;
 }
예제 #9
0
 /**
  *
  */
 public function setRoute($uri = null, $verb = null)
 {
     // Build the URI
     if (empty($uri) === true) {
         if (\z\app()->isCli() === true) {
             if (array_key_exists(1, $GLOBALS['argv']) === true) {
                 $this->_uri = $GLOBALS['argv'][1];
             }
         } else {
             $this->_uri = explode('?', \z\request()->server('REQUEST_URI'), 2)[0];
         }
     }
     // Build the verb
     if (empty($verb) === true) {
         if (\z\app()->isCli() === true) {
             $this->_verb = 'CLI';
         } else {
             $this->_verb = \z\request()->server('REQUEST_METHOD');
         }
     }
     // Try to find the URI/verb in definitions
     foreach ($this->_definitions as $uri => $definition) {
         // Is the verb supported?
         if (array_key_exists($this->_verb, $definition['verbs']) === false) {
             continue;
         }
         // Get the verb
         $verb = $definition['verbs'][$this->_verb];
         // Replace URI arguments by their pattern
         $uriFragments = explode('/', $uri);
         foreach ($uriFragments as $key => &$uriFragment) {
             if (preg_match('/\\{([a-zA-Z0-9]+)\\}/', $uriFragment, $matches) === 1 && array_key_exists($matches[1], $definition['arguments']) === true) {
                 $uriFragment = '(' . $definition['arguments'][$matches[1]]['pattern'] . ')';
             }
         }
         // Does the URI match?
         $pattern = '/^' . str_replace('/', '\\/', implode('/', $uriFragments)) . '\\/?$/';
         if (preg_match($pattern, $this->_uri, $matches) !== 1) {
             continue;
         }
         // Remove the first match
         array_shift($matches);
         // Arguments are remaining matches
         $arguments = $matches;
         // Count arguments
         $nbArguments = count($arguments);
         $nbArgumentsDefined = count($definition['arguments']);
         // Store arguments into definition
         if ($nbArguments > 0 && $nbArguments === $nbArgumentsDefined) {
             $keys = array_keys($definition['arguments']);
             foreach ($arguments as $key => $value) {
                 $definition['arguments'][$keys[$key]]['value'] = $value;
             }
         }
         // This is the route!
         $this->_route = array_merge($verb, ['arguments' => $definition['arguments'], 'post' => $definition['post'], 'pre' => $definition['pre']]);
         break;
     }
     // Do we have a route?
     if (is_null($this->_route) === true) {
         // Build definitions
         $definitions = [];
         foreach ($this->_definitions as $definitionCode => $definition) {
             $definitions[$definitionCode] = ['verbs' => array_keys($definition['verbs'])];
         }
         // Throw the exception
         \z\e(EXCEPTION_ROUTE_NOT_FOUND, ['uri' => $this->_uri, 'verb' => $this->_verb, 'definitions' => $definitions]);
     }
 }