Exemple #1
0
    /**
     * Constructor
     *
     * This constructor fetches the local configuration path
     * and constructs the config file for a certain module.
     *
     * After having the xml configuration file loaded, it'll create
     * a Zend_Config_Xml instance and store it in $this->config
     *
     * @param string $name  The name of the configuration to load
     */
    public function __construct($name)
    {
        if (defined('ADMIN_CONF_PATH')) {
            $config = ADMIN_CONF_PATH;
        } else {
            try {
                $config = Zend_Registry::get('localConfigPath');
            } catch (Zend_Exception $e) {}
        }

        $this->configFile = $config . $name . '.xml';
        $this->configName = $name;

        /**
         * @TODO Remove me after the rewrite of the Config Reader. Move this to
         *       Frapi_Internal. I dislike inter-dependencies.
         */
        if ($cachedConfig = Frapi_Internal::getCached('configFile-' . $name))
        {
            $this->config = $cachedConfig;
        } else {
            $helper = new Lupin_Config_Helper_XmlArray();
            $this->config = $helper->parse($this->configFile);
            Frapi_Internal::setCached('configFile-' . $name, $this->config);
        }

        $this->config = $this->config[$name];
    }
Exemple #2
0
 public function testIsPartnerExpectTrue()
 {
     $partner = array('*****@*****.**' => array('email' => '*****@*****.**', 'api_key' => 'e5b9e917648c57a978ba633095cb7a12fb0a647e'));
     Frapi_Internal::setCached('Partners.emails-keys', $partner);
     $partner = Frapi_Model_Partner::isPartner('*****@*****.**', 'e5b9e917648c57a978ba633095cb7a12fb0a647e');
     $this->assertTrue($partner);
     Frapi_Internal::deleteCached('Partners.emails-keys');
 }
Exemple #3
0
 /**
  * Load routes from APC, database etc.
  * Prepare routes if necessary!
  *
  * @return void
  */
 public function loadAndPrepareRoutes()
 {
     if ($routes = Frapi_Internal::getCached('Router.routes-prepared')) {
         $this->setPreparedRoutes($routes);
     } else {
         $routes = array();
         $ret = Frapi_Internal::getConfiguration('actions');
         $rows = $ret->getAll('action');
         foreach ($rows as $row) {
             if (isset($row['route']) && !empty($row['route'])) {
                 $routes[$row['name']] = $row['route'];
             }
         }
         $this->setPreparedRoutes($preparedRoutes = self::prepareRoutes($routes));
         Frapi_Internal::setCached('Router.routes-prepared', $preparedRoutes);
     }
 }
Exemple #4
0
 /**
  * Get default format from SQLite database
  *
  * A format (output type) has not been supplied
  * so try to get default from backend.
  *
  * @return String The format.
  */
 public function getDefaultFormatFromConfiguration()
 {
     if ($default_output_format = Frapi_Internal::getCached('Output.default-format')) {
         return $default_output_format;
     }
     $conf = Frapi_Internal::getConfiguration('outputs');
     $row = $conf->getByField('output', 'default', '1');
     if (isset($row) && isset($row['name'])) {
         Frapi_Internal::setCached('Output.default-format', $row['name']);
         return $row['name'];
     }
     return Frapi_Controller_Api::DEFAULT_OUTPUT_FORMAT;
 }
Exemple #5
0
    /**
     * Private function to get error (statically, APC, or DB).
     *
     * This function tries to locate the error in progressively
     * slower datastores (static class variable, APC, database)
     * and will store the loaded errors in the faster stores.
     *
     * @param string $error_name Name of error.
     * @param string $error_msg  The actual message of the error.
     * @param int    $http_code  This might be hard to grasp however, we are in a web
     *                           industry dealing with the web. The code you are sending
     *                           to your exception should really be represented by the
     *                           HTTP Code returned to your users.
     *
     * @return array An array with the content of the error.
     */
    private static function _get($error_name, $error_msg = false, $http_code = false)
    {
        if (!self::$_statically_loaded) {
            $errors = Frapi_Internal::getCached('Errors.user-defined');

            if ($errors) {
                self::$_errors = $errors;
            } elseif ($errors = self::_getErrorsFromDb()) {
                self::$_errors = $errors;
                Frapi_Internal::setCached('Errors.user-defined', $errors);
            }

            self::$_statically_loaded = true;
        }

        if (isset(self::$_errors[$error_name])) {
            $error = self::$_errors[$error_name];

            if ($error_msg !== false) {
                $error['message'] = $error_msg;
            }

            if ($http_code !== false) {
                $error['http_code'] = $http_code;
            }

            return $error;
        }

        return array(
            'name'      => $error_name,
            'message'   => $error_msg !== false ? $error_msg : $error_name,
            'http_code' => $http_code !== false  ? $http_code : '400',
        );
    }
Exemple #6
0
 /**
  * Private function to get error (statically, APC, or DB).
  *
  * This function tries to locate the error in progressively
  * slower datastores (static class variable, APC, database)
  * and will store the loaded errors in the faster stores.
  *
  * @param string $error_name Name of error.
  * @param string $error_msg   The actual message of the error.
  * @param int    $http_code   This might be hard to grasp however, we are in a web
  *                            industry dealing with the web. The code you are sending
  *                            to your exception should really be represented by the
  *                            HTTP Code returned to your users.
  * @param string $http_phrase The http phrase associated with the http_code
  *
  * @return array An array with the content of the error.
  */
 private static function _get($error_name, $error_msg = false, $http_code = false, $http_phrase = false)
 {
     if (!self::$_statically_loaded) {
         $errors = Frapi_Internal::getCached('Errors.user-defined');
         if ($errors) {
             self::$_errors = $errors;
         } elseif ($errors = self::_getErrorsFromDb()) {
             self::$_errors = $errors;
             Frapi_Internal::setCached('Errors.user-defined', $errors);
         }
         self::$_statically_loaded = true;
     }
     if (isset(self::$_errors[$error_name])) {
         $error = self::$_errors[$error_name];
         if ($error_msg !== false) {
             $error['message'] = $error_msg;
         }
         if ($http_code !== false) {
             $error['http_code'] = $http_code;
         }
         if ($http_phrase !== false) {
             $error['http_phrase'] = $http_phrase;
         } else {
             if (isset(Frapi_Response::$http_reason[$error['http_code']]) && !$error['http_phrase']) {
                 $error['http_phrase'] = Frapi_Response::$http_reason[$error['http_code']];
             } else {
                 if ($error['message'] !== false && !$error['http_phrase']) {
                     $error['http_phrase'] = $error['message'];
                 }
             }
         }
         return $error;
     }
     if ($http_phrase === false) {
         if (isset(Frapi_Response::$http_reason[$http_code])) {
             $http_phrase = Frapi_Response::$http_reason[$http_code];
         } else {
             if ($error_msg !== false) {
                 $http_phrase = $error_msg;
             }
         }
     }
     return array('name' => $error_name, 'message' => $error_msg !== false ? $error_msg : $error_name, 'http_code' => $http_code !== false ? $http_code : '400', 'http_phrase' => $http_phrase !== false ? $http_phrase : 'Bad Request');
 }
Exemple #7
0
 /**
  * Populate the Output
  *
  * This method populates the $this->response
  * variable with the value returned from the
  * action.
  *
  * @param Mixed  $response Most of the times an array but could be and stdClass
  * @param String $customTemplate The custom template file to use instead of the default one.
  *
  * @return Object $This object
  */
 public function populateOutput($data, $customTemplate = false)
 {
     $directory = CUSTOM_OUTPUT . DIRECTORY_SEPARATOR . 'xml';
     $file = $directory . DIRECTORY_SEPARATOR . ucfirst(strtolower($this->action)) . '.xml.tpl';
     if ($customTemplate !== false) {
         $file = $directory . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . $customTemplate . '.xml.tpl';
     }
     $xml = '';
     if (!is_array($data)) {
         $data = $this->_normalizeToArray($data);
     }
     $print = hash('md5', json_encode($data + array('__action__name' => $this->action)));
     if ($response = Frapi_Internal::getCached($print)) {
         $this->response = json_decode($response);
     } elseif (file_exists($file)) {
         ob_start();
         echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
         include $file;
         $xml = ob_get_contents();
         ob_end_clean();
         $this->response = $xml;
         Frapi_Internal::setCached($print, json_encode($xml));
     } elseif ($this->action == 'defaultError') {
         $directory = LIBRARY_OUTPUT . DIRECTORY_SEPARATOR . 'xml';
         $file = $directory . DIRECTORY_SEPARATOR . 'Defaulterror.xml.tpl';
         ob_start();
         echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
         include $file;
         $xml = ob_get_contents();
         ob_end_clean();
         $this->response = $xml;
         Frapi_Internal::setCached($print, json_encode($xml));
     } else {
         $this->response = $this->_generateXML($data);
     }
     return $this;
 }
Exemple #8
0
 public function setUp()
 {
     Frapi_Internal::setCached('Output.mimeMaps', array('application/xml' => 'xml', 'text/xml' => 'xml', 'application/json' => 'json', 'text/json' => 'json', 'text/html' => 'html', 'text/plain' => 'json', 'application/javascript' => 'js', 'text/javascript' => 'js', 'text/php-printr' => 'printr', 'application/vnd.test.:format' => ':format', 'application/vnd.test.:version.:format' => ':format', 'application/vnd.test.:version+json' => 'json', 'text/csv' => 'csv'));
 }
Exemple #9
0
 public static function getEnabledFormats()
 {
     try {
         if ($formats = Frapi_Internal::getCached('Output.formats-enabled')) {
             return $formats;
         }
         $cache = new Frapi_Internal();
         $outputs = $cache->getConfiguration('outputs')->getAll('output');
         $formats = array();
         foreach ($outputs as $output) {
             if ($output['enabled'] == 1) {
                 $formats[] = strtolower($output['name']);
             }
         }
     } catch (Exception $e) {
         return false;
     }
     Frapi_Internal::setCached('Output.formats-enabled', $formats);
     return $formats;
 }
Exemple #10
0
 /**
  * Test cache stores and retrieves items.
  *
  * @dataProvider keysValuesProvider
  **/
 public function testCacheStoreAndRetrieve($key, $value)
 {
     Frapi_Internal::setCached($key, $value);
     $this->assertEquals($value, Frapi_Internal::getCached($key));
 }