Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

createRegistry public

  • Loading branch information...
commit 84038a23cb4be5cc7d864f8df62941809d346e1d 1 parent ee59402
Amy Stephen authored
View
247 source/Molajo/Service/Services/RegistryService.php
@@ -80,32 +80,9 @@ public function __construct($global = false)
}
/**
- * Global
- *
- * @return object
- * @since 1.0
- */
- public function createGlobalRegistry()
- {
- /** initialise known namespaces for application */
- $xml = CONFIGURATION_FOLDER . '/Application/registry.xml';
- if (is_file($xml)) {
- } else {
- return false;
- }
-
- $list = simplexml_load_file($xml);
-
- foreach ($list->registry as $item) {
- $reg = $this->createRegistry((string)$item);
- }
- }
-
- /**
* Create a Registry array for specified Namespace
*
- * Usage:
- * Services::Registry()->createRegistry('namespace');
+ * This is protected as the class automatically creates namespaces that are referenced, but do not exist
*
* @param $namespace
*
@@ -118,7 +95,6 @@ public function createRegistry($namespace)
}
/** Keys array */
-// $i = count($this->registryKeys);
$this->registryKeys[] = $namespace;
/** Namespace array */
@@ -128,52 +104,11 @@ public function createRegistry($namespace)
}
/**
- * Delete a Registry for specified Namespace
- *
- * Usage:
- * Services::Registry()->deleteRegistry('namespace');
- *
- * @param $namespace
- *
- * @return array
- */
- public function deleteRegistry($namespace)
- {
- if (isset($this->registryKeys[$namespace])) {
- unset($this->registryKeys[$namespace]);
- }
- if (isset($this->registry[$namespace])) {
- unset($this->registry[$namespace]);
- }
-
- return $this;
- }
-
- /**
- * Returns the entire registry for the specified namespace
- *
- * Usage:
- * Services::Registry()->getRegistry('namespace');
- *
- * @param $namespace
- *
- * @return array
- */
- public function getRegistry($namespace)
- {
- if (in_array($namespace, $this->registryKeys)) {
- return $this->registry[$namespace];
- } else {
- return $this->createRegistry($namespace);
- }
- }
-
- /**
* Returns a Parameter property for a specific item and namespace registry
* Alias for JFactory::getConfig, returning full registry set for local use
*
* Usage:
- * Services::Registry()->get('Request', 'parameter_name');
+ * Services::Registry()->get('Name Space', 'key value');
*
* @param string $namespace
* @param string $key
@@ -184,10 +119,12 @@ public function getRegistry($namespace)
*/
public function get($namespace, $key = null, $default = null)
{
+ /** Get without a key returns the entire namespace (Like JFactory::getConfig) */
if ($key == null) {
return $this->getRegistry($namespace);
}
+ /** No sense in fighting it. */
$key = strtolower($key);
/** Does registry exist? If not, create it. */
@@ -196,22 +133,22 @@ public function get($namespace, $key = null, $default = null)
$this->createRegistry($namespace);
}
- /** should exist */
+ /** If it doesn't exist, we have problems. */
if (isset($this->registry[$namespace])) {
} else {
- //throw error
+ //todo: throw error
echo $namespace . ' Blow up in RegistryService';
die;
}
- /** Retrieve the namespace */
+ /** Retrieve the registry for the namespace */
$array = $this->registry[$namespace];
if (is_array($array)) {
} else {
$array = array();
}
- /** Is there a match? */
+ /** Look for the key value requested */
$found = false;
while (list($existingKey, $existingValue) = each($array)) {
if (strtolower($existingKey) == strtolower($key)) {
@@ -220,6 +157,7 @@ public function get($namespace, $key = null, $default = null)
}
}
+ /** Create the entry, if not found, and set it to default */
if ($found == true) {
} else {
@@ -227,6 +165,7 @@ public function get($namespace, $key = null, $default = null)
$this->registry[$namespace] = $array;
}
+ /** That's what you wanted, right? */
return $array[$key];
}
@@ -234,7 +173,7 @@ public function get($namespace, $key = null, $default = null)
* Sets a Parameter property for a specific item and parameter set
*
* Usage:
- * Services::Registry()->set('Request', 'parameter_name', $value);
+ * Services::Registry()->set('Name Space', 'key_name', $value);
*
* @param string $namespace
* @param string $key
@@ -245,6 +184,7 @@ public function get($namespace, $key = null, $default = null)
*/
public function set($namespace, $key, $value = '')
{
+ /** keep it all on the downlow */
$key = strtolower($key);
/** Get the Registry (or create it) */
@@ -260,10 +200,10 @@ public function set($namespace, $key, $value = '')
}
/**
- * Copy one global registry to another
+ * Copy one namespace registry to another
*
* Usage:
- * Services::Registry()->copy('x', 'y');
+ * Services::Registry()->copy('namespace-x', 'to-namespace-y');
*
* @param $copyThis
* @param $intoThis
@@ -273,10 +213,10 @@ public function set($namespace, $key, $value = '')
*/
public function copy($copyThis, $intoThis)
{
- /** Get the Registry that will be copied */
+ /** Get (or create) the Registry that will be copied */
$copy = $this->getRegistry($copyThis);
- /** Get the Registry that will be copied into */
+ /** Get (or create) the Registry that will be copied to */
$into = $this->getRegistry($intoThis);
/** Save the new registry */
@@ -286,10 +226,29 @@ public function copy($copyThis, $intoThis)
}
/**
+ * Delete a Registry for specified Namespace
+ *
+ * @param $namespace
+ *
+ * @return array
+ */
+ public function deleteRegistry($namespace)
+ {
+ if (isset($this->registryKeys[$namespace])) {
+ unset($this->registryKeys[$namespace]);
+ }
+ if (isset($this->registry[$namespace])) {
+ unset($this->registry[$namespace]);
+ }
+
+ return $this;
+ }
+
+ /**
* Returns an array containing key and name pairs for a namespace registry
*
* Usage:
- * Services::Registry()->getArray('request');
+ * Services::Registry()->getArray('Name Space');
*
* @param string $namespace
* @param boolean @keyOnly set to true to retrieve key names
@@ -302,6 +261,7 @@ public function getArray($namespace, $keyOnly = false)
/** Get the Registry */
$array = $this->getRegistry($namespace);
+ /** full registry array requested */
if ($keyOnly == false) {
return $array;
}
@@ -339,7 +299,35 @@ public function loadArray($namespace, $array = array())
}
/**
- * Loads JSON data from a field given the field xml definition
+ * Retrieves a list of ALL namespaced registries and optionally keys/values
+ *
+ * Usage:
+ * Services::Registry()->listRegistry(1);
+ *
+ * @param boolean $all true - returns the entire list and each registry
+ * false - returns a list of registry names, only
+ *
+ * @return mixed|boolean or array
+ * @since 1.0
+ */
+ public function listRegistry($include_entries = false)
+ {
+ if ($include_entries == false) {
+ return $this->registryKeys;
+ }
+
+ $nsArray = array();
+
+ while (list($nsName, $nsValue) = each($this->registryKeys)) {
+ $nsArray['namespace'] = $nsValue;
+ $nsArray['registry'] = $this->registry[$nsValue];
+ }
+
+ return $nsArray;
+ }
+
+ /**
+ * Loads JSON data from a field given the field xml definition - filters input
* (can be used for fields like registry, custom fields, metadata, etc.)
*
* Usage:
@@ -395,7 +383,7 @@ public function loadField($namespace, $field_name, $data, $xml)
$set = $default;
}
/** Filter Input and Save the Registry */
- //$set = $this->filterInput($name, $set, $dataType);
+//todo: come back when HTML Purifier namespacing addressed -- $set = $this->filterInput($name, $set, $dataType);
$nsArray[$name] = $set;
$this->registry[$namespace] = $nsArray;
}
@@ -405,39 +393,63 @@ public function loadField($namespace, $field_name, $data, $xml)
}
/**
- * Retrieves a list of namespaced registries and optionally keys/values
- *
- * Usage:
- * Services::Registry()->listRegistry(1);
+ * filterInput
*
- * @param boolean $all true - returns the entire list and each registry
- * false - returns a list of registry names, only
+ * @param string $name Name of input field
+ * @param string $field_value Value of input field
+ * @param string $dataType Datatype of input field
+ * @param int $null 0 or 1 - is null allowed
+ * @param string $default Default value, optional
*
- * @return mixed|boolean or array
+ * @return mixed
* @since 1.0
- *
- *
*/
- public function listRegistry($include_entries = false)
+ protected function filterInput(
+ $name, $value, $dataType, $null = null, $default = null)
{
- if ($include_entries == false) {
- return $this->registryKeys;
- }
- $nsArray = array();
+ try {
+ $value = Services::Filter()
+ ->filter(
+ $value,
+ $dataType,
+ $null,
+ $default
+ );
- while (list($nsName, $nsValue) = each($this->registryKeys)) {
- $nsArray['namespace'] = $nsValue;
- $nsArray['registry'] = $this->registry[$nsValue];
+ } catch (\Exception $e) {
+ //todo: errors
+ echo $e->getMessage() . ' ' . $name;
}
- return $nsArray;
+ return $value;
+ }
+
+ /**
+ * Create Global Registry - activated in Services during Service startup to initialize the global space
+ *
+ * @return object
+ * @since 1.0
+ */
+ protected function createGlobalRegistry()
+ {
+//todo: build in spl file priorities and pass in file
+
+ $list = $this->loadFile('registry');
+
+ foreach ($list->registry as $item) {
+ $reg = $this->createRegistry((string)$item);
+ }
}
/**
- * loadFile
+ * loadFile is the isolated point in the application where all XML configuration files are read
+ * That includes XML for tables, services, and the application, along with service startup
+ *
+ * Usage:
+ * Services::Registry()->loadFile('Content', 'Table');
*
- * add php spl priority for loading
+ * todo: add php spl priority for loading and a little more thinking on API options (ini? json?)
*
* @return object
* @since 1.0
@@ -466,35 +478,22 @@ public static function loadFile($file, $type = 'Application')
}
/**
- * filterInput
+ * Returns the entire registry for the specified namespace
*
- * @param string $name Name of input field
- * @param string $field_value Value of input field
- * @param string $dataType Datatype of input field
- * @param int $null 0 or 1 - is null allowed
- * @param string $default Default value, optional
+ * This is protected as the class will retrieve the registry with a get on namespace, only
*
- * @return mixed
- * @since 1.0
+ * Services::Registry()->get('Name Space');
+ *
+ * @param $namespace
+ *
+ * @return array
*/
- protected function filterInput(
- $name, $value, $dataType, $null = null, $default = null)
+ protected function getRegistry($namespace)
{
-
- try {
- $value = Services::Filter()
- ->filter(
- $value,
- $dataType,
- $null,
- $default
- );
-
- } catch (\Exception $e) {
- //todo: errors
- echo $e->getMessage() . ' ' . $name;
+ if (in_array($namespace, $this->registryKeys)) {
+ return $this->registry[$namespace];
+ } else {
+ return $this->createRegistry($namespace);
}
-
- return $value;
}
}
View
500 source/Vendor/ChromePHP/RegistryService.php
@@ -0,0 +1,500 @@
+<?php
+/**
+ * @package Molajo
+ * @copyright 2012 Amy Stephen. All rights reserved.
+ * @license GNU General Public License Version 2, or later http://www.gnu.org/licenses/gpl.html
+ */
+
+namespace Molajo\Service\Services;
+
+use Molajo\Service\Services;
+
+defined('MOLAJO') or die;
+
+/**
+ * Registry
+ *
+ * @package Molajo
+ * @subpackage Services
+ * @since 1.0
+ */
+Class RegistryService
+{
+ /**
+ * $instance
+ *
+ * @var object
+ * @since 1.0
+ */
+ protected static $instance;
+
+ /**
+ * Array containing the key to each $registry object
+ *
+ * @var Object Array
+ * @since 1.0
+ */
+ protected $registryKeys = array();
+
+ /**
+ * Array containing all globally defined $registry objects
+ *
+ * @var Object Array
+ * @since 1.0
+ */
+ protected $registry = array();
+
+ /**
+ * getInstance
+ *
+ * @static
+ * @return bool|object
+ * @since 1.0
+ */
+ public static function getInstance()
+ {
+ if (empty(self::$instance)) {
+ self::$instance = new RegistryService(true);
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Initialise known namespaces for application
+ *
+ * @return object
+ * @since 1.0
+ */
+ public function __construct($global = false)
+ {
+ /** store all registries in this object */
+ $this->registry = array();
+ $this->registryKeys = array();
+
+ if ($global == true) {
+ $this->createGlobalRegistry();
+ }
+
+ return $this;
+ }
+
+ /**
+ * Global
+ *
+ * @return object
+ * @since 1.0
+ */
+ public function createGlobalRegistry()
+ {
+ /** initialise known namespaces for application */
+ $xml = CONFIGURATION_FOLDER . '/Application/registry.xml';
+ if (is_file($xml)) {
+ } else {
+ return false;
+ }
+
+ $list = simplexml_load_file($xml);
+
+ foreach ($list->registry as $item) {
+ $reg = $this->createRegistry((string)$item);
+ }
+ }
+
+ /**
+ * Create a Registry array for specified Namespace
+ *
+ * Usage:
+ * Services::Registry()->createRegistry('namespace');
+ *
+ * @param $namespace
+ *
+ * @return array
+ */
+ public function createRegistry($namespace)
+ {
+ if (in_array($namespace, $this->registryKeys)) {
+ return $this->registry[$namespace];
+ }
+
+ /** Keys array */
+// $i = count($this->registryKeys);
+ $this->registryKeys[] = $namespace;
+
+ /** Namespace array */
+ $this->registry[$namespace] = array();
+
+ return $this->registry[$namespace];
+ }
+
+ /**
+ * Delete a Registry for specified Namespace
+ *
+ * Usage:
+ * Services::Registry()->deleteRegistry('namespace');
+ *
+ * @param $namespace
+ *
+ * @return array
+ */
+ public function deleteRegistry($namespace)
+ {
+ if (isset($this->registryKeys[$namespace])) {
+ unset($this->registryKeys[$namespace]);
+ }
+ if (isset($this->registry[$namespace])) {
+ unset($this->registry[$namespace]);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Returns the entire registry for the specified namespace
+ *
+ * Usage:
+ * Services::Registry()->getRegistry('namespace');
+ *
+ * @param $namespace
+ *
+ * @return array
+ */
+ public function getRegistry($namespace)
+ {
+ if (in_array($namespace, $this->registryKeys)) {
+ return $this->registry[$namespace];
+ } else {
+ return $this->createRegistry($namespace);
+ }
+ }
+
+ /**
+ * Returns a Parameter property for a specific item and namespace registry
+ * Alias for JFactory::getConfig, returning full registry set for local use
+ *
+ * Usage:
+ * Services::Registry()->get('Request', 'parameter_name');
+ *
+ * @param string $namespace
+ * @param string $key
+ * @param mixed $default
+ *
+ * @return mixed registry value
+ * @since 1.0
+ */
+ public function get($namespace, $key = null, $default = null)
+ {
+ if ($key == null) {
+ return $this->getRegistry($namespace);
+ }
+
+ $key = strtolower($key);
+
+ /** Does registry exist? If not, create it. */
+ if (in_array($namespace, $this->registryKeys)) {
+ } else {
+ $this->createRegistry($namespace);
+ }
+
+ /** should exist */
+ if (isset($this->registry[$namespace])) {
+ } else {
+ //throw error
+ echo $namespace . ' Blow up in RegistryService';
+ die;
+ }
+
+ /** Retrieve the namespace */
+ $array = $this->registry[$namespace];
+ if (is_array($array)) {
+ } else {
+ $array = array();
+ }
+
+ /** Is there a match? */
+ $found = false;
+ while (list($existingKey, $existingValue) = each($array)) {
+ if (strtolower($existingKey) == strtolower($key)) {
+ $found = true;
+ break;
+ }
+ }
+
+ if ($found == true) {
+
+ } else {
+ $array[$key] = $default;
+ $this->registry[$namespace] = $array;
+ }
+
+ return $array[$key];
+ }
+
+ /**
+ * Sets a Parameter property for a specific item and parameter set
+ *
+ * Usage:
+ * Services::Registry()->set('Request', 'parameter_name', $value);
+ *
+ * @param string $namespace
+ * @param string $key
+ * @param mixed $default
+ *
+ * @return Registry
+ * @since 1.0
+ */
+ public function set($namespace, $key, $value = '')
+ {
+ $key = strtolower($key);
+
+ /** Get the Registry (or create it) */
+ $array = $this->getRegistry($namespace);
+
+ /** Set the value for the key */
+ $array[$key] = $value;
+
+ /** Save the registry */
+ $this->registry[$namespace] = $array;
+
+ return $this;
+ }
+
+ /**
+ * Copy one global registry to another
+ *
+ * Usage:
+ * Services::Registry()->copy('x', 'y');
+ *
+ * @param $copyThis
+ * @param $intoThis
+ *
+ * @return mixed
+ * @since 1.0
+ */
+ public function copy($copyThis, $intoThis)
+ {
+ /** Get the Registry that will be copied */
+ $copy = $this->getRegistry($copyThis);
+
+ /** Get the Registry that will be copied into */
+ $into = $this->getRegistry($intoThis);
+
+ /** Save the new registry */
+ $this->registry[$intoThis] = $copy;
+
+ return $this;
+ }
+
+ /**
+ * Returns an array containing key and name pairs for a namespace registry
+ *
+ * Usage:
+ * Services::Registry()->getArray('request');
+ *
+ * @param string $namespace
+ * @param boolean @keyOnly set to true to retrieve key names
+ *
+ * @return array
+ * @since 1.0
+ */
+ public function getArray($namespace, $keyOnly = false)
+ {
+ /** Get the Registry */
+ $array = $this->getRegistry($namespace);
+
+ if ($keyOnly == false) {
+ return $array;
+ }
+
+ /* Key only */
+ $keyArray = array();
+ while (list($key, $value) = each($array)) {
+ $keyArray[] = $key;
+ }
+
+ return $keyArray;
+ }
+
+ /**
+ * Populates a registry with an array of key and name pairs
+ *
+ * Usage:
+ * Services::Registry()->loadArray('Request', $array);
+ *
+ * @param string $name name of registry to use or create
+ * @param boolean $array key and value pairs to load
+ *
+ * @return array
+ * @since 1.0
+ */
+ public function loadArray($namespace, $array = array())
+ {
+ /** Get the Registry that will be copied */
+ $this->getRegistry($namespace);
+
+ /** Save the new registry */
+ $this->registry[$namespace] = $array;
+
+ return $this;
+ }
+
+ /**
+ * Loads JSON data from a field given the field xml definition
+ * (can be used for fields like registry, custom fields, metadata, etc.)
+ *
+ * Usage:
+ * Services::Registry()->loadField('Namespace', 'field_name', $results['field_name'], $xml->field_group);
+ *
+ * @param $namespace
+ * @param $field_name
+ * @param $data
+ * @param $xml
+ *
+ * @return null
+ * @since 1.0
+ */
+ public function loadField($namespace, $field_name, $data, $xml)
+ {
+ /** Get the Registry that will be copied */
+ $this->getRegistry($namespace);
+
+ $nsArray = array();
+
+ /** Decode JSON into object */
+ $jsonObject = json_decode($data);
+
+ /** Place field names into named pair array */
+ $lookup = array();
+
+ if (count($jsonObject) > 0) {
+ foreach ($jsonObject as $key => $value) {
+ $lookup[$key] = $value;
+ }
+ }
+
+ /** Load data for defined Fields, only */
+ if (isset($xml->$field_name)) {
+
+ foreach ($xml->$field_name as $cf) {
+
+ $name = (string)$cf['name'];
+ $name = strtolower($name);
+ $dataType = (string)$cf['filter'];
+ $null = (string)$cf['null'];
+ $default = (string)$cf['default'];
+ $values = (string)$cf['values'];
+
+ if ($default == '') {
+ $default = null;
+ }
+
+ /** Use value, if exists, or defined default */
+ if (isset($lookup[$name])) {
+ $set = $lookup[$name];
+ } else {
+ $set = $default;
+ }
+ /** Filter Input and Save the Registry */
+ //$set = $this->filterInput($name, $set, $dataType);
+ $nsArray[$name] = $set;
+ $this->registry[$namespace] = $nsArray;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Retrieves a list of namespaced registries and optionally keys/values
+ *
+ * Usage:
+ * Services::Registry()->listRegistry(1);
+ *
+ * @param boolean $all true - returns the entire list and each registry
+ * false - returns a list of registry names, only
+ *
+ * @return mixed|boolean or array
+ * @since 1.0
+ *
+ *
+ */
+ public function listRegistry($include_entries = false)
+ {
+ if ($include_entries == false) {
+ return $this->registryKeys;
+ }
+
+ $nsArray = array();
+
+ while (list($nsName, $nsValue) = each($this->registryKeys)) {
+ $nsArray['namespace'] = $nsValue;
+ $nsArray['registry'] = $this->registry[$nsValue];
+ }
+
+ return $nsArray;
+ }
+
+ /**
+ * loadFile
+ *
+ * add php spl priority for loading
+ *
+ * @return object
+ * @since 1.0
+ * @throws \RuntimeException
+ */
+ public static function loadFile($file, $type = 'Application')
+ {
+ if ($type == 'Application' || $type == 'Table') {
+ $path_and_file = CONFIGURATION_FOLDER . '/' . $type . '/' . $file . '.xml';
+ } else {
+ $path_and_file = $type . '/' . $file . '.xml';
+ }
+
+ if (file_exists($path_and_file)) {
+ } else {
+ throw new \RuntimeException('File not found: ' . $path_and_file);
+ }
+
+ try {
+ return simplexml_load_file($path_and_file);
+
+ } catch (\Exception $e) {
+
+ throw new \RuntimeException ('Failure reading XML File: ' . $path_and_file . ' ' . $e->getMessage());
+ }
+ }
+
+ /**
+ * filterInput
+ *
+ * @param string $name Name of input field
+ * @param string $field_value Value of input field
+ * @param string $dataType Datatype of input field
+ * @param int $null 0 or 1 - is null allowed
+ * @param string $default Default value, optional
+ *
+ * @return mixed
+ * @since 1.0
+ */
+ protected function filterInput(
+ $name, $value, $dataType, $null = null, $default = null)
+ {
+
+ try {
+ $value = Services::Filter()
+ ->filter(
+ $value,
+ $dataType,
+ $null,
+ $default
+ );
+
+ } catch (\Exception $e) {
+ //todo: errors
+ echo $e->getMessage() . ' ' . $name;
+ }
+
+ return $value;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.