Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
  • 4 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
80 README.md
View
@@ -1,4 +1,82 @@
createphp
=========
-PHP adapter for CreateJS
+This is a small standalone library designed to make it easier to integrate CreateJS
+into existing PHP applications/frameworks.
+
+Usage
+-----
+
+To use createphp, you need to implement the rdfMapper class, instantiate it with a
+configuration for your data source and then you're good to go
+
+```php
+$object = load_your_data_from_somewhere();
+
+$config = array
+(
+ 'blog_article' => array(
+ 'storage' => 'some_db_table',
+ 'attributes' => array(
+ 'typeof' => 'sioc:Blog',
+ ),
+ 'vocabularies' => array(
+ 'dcterms' => 'http://purl.org/dc/terms/',
+ 'sioc' => 'http://rdfs.org/sioc/ns#'
+ ),
+ 'properties' => array(
+ 'title' => array
+ (
+ 'rdf_name' => 'dcterms:title',
+ ),
+ 'content' => array(
+ 'rdf_name' => 'sioc:content',
+ ),
+ ),
+ )
+);
+
+$mapper = new my_mapper_class(new createphp\config($config));
+$controller = new createphp\controller($mapper);
+
+echo $controller
+```
+
+This will output something like
+
+```html
+<div about="http://some_domain.com/some_unique_identifier"
+ xmlns:dcterms="http://purl.org/dc/terms/"
+ xmlns:sioc="http://rdfs.org/sioc/ns#"
+ typeof="sioc:Blog">
+
+ <div property="dcterms:title">
+ Some title
+ </div>
+
+ <div property="sioc:content">
+ Article content
+ </div>
+</div>
+```
+
+Of course, the markup is completely configurable, and you can also render the
+individual fields separately. If you include the CreateJS files into your page,
+all specified fields will become editable. To actually save the data, you will
+have to provide an access point for the REST service, like so:
+
+```php
+$config = new createphp\config(load_my_configuration_from_somewhere());
+$mapper = new my_mapper_class($config);
+
+$received_data = json_decode(file_get_contents("php://input"), true);
+$service = new createphp\restservice($mapper, $received_data);
+
+$service->run();
+```
+
+Word of Warning
+---------------
+The code is still very much in development. While it's kept in a constantly running
+state, please note that the API might still change considerably. Suggestions and
+feedback are of course welcome!
144 collection.php
View
@@ -0,0 +1,144 @@
+<?php
+/**
+ * Collection holder
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+class collection extends node implements \ArrayAccess, \Iterator
+{
+ protected $_position = 0;
+
+ protected $_config;
+ protected $_controller;
+
+ public function __construct(array $settings, controller $parent)
+ {
+ $this->_config = $settings;
+
+ foreach ($settings['attributes'] as $key => $value)
+ {
+ $this->set_attribute($key, $value);
+ }
+
+ $this->_controller = $parent;
+ $parent_mapper = $this->_controller->get_mapper();
+ $parent_object = $this->_controller->get_object();
+ $mapper_class = get_class($parent_mapper);
+ $config = $parent_mapper->get_config();
+ $config->set_schema($settings['type'][0]);
+
+ $children = $parent_mapper->get_children($parent_object, $config);
+
+ if ($parent_mapper->is_editable($parent_object))
+ {
+ $object = $parent_mapper->prepare_object($config, $parent_object);
+ array_unshift($children, $object);
+ }
+
+ // create controllers for children
+ foreach ($children as $child)
+ {
+ $mapper = new $mapper_class($config);
+ $controller = new controller($mapper, $this->_parent);
+ $controller->set_object($child, $settings['type'][0]);
+ $this->_children[] = $controller;
+ }
+ }
+
+ function rewind()
+ {
+ $this->_position = 0;
+ }
+
+ function current()
+ {
+ return $this->_children[$this->_position];
+ }
+
+ function key()
+ {
+ return $this->_position;
+ }
+
+ function next()
+ {
+ ++$this->_position;
+ }
+
+ function valid()
+ {
+ return isset($this->_children[$this->_position]);
+ }
+
+ public function offsetSet($offset, $value)
+ {
+ if (is_null($offset))
+ {
+ $this->_children[] = $value;
+ }
+ else
+ {
+ $this->_children[$offset] = $value;
+ }
+ }
+
+ public function offsetExists($offset)
+ {
+ return isset($this->_children[$offset]);
+ }
+
+ public function offsetUnset($offset)
+ {
+ unset($this->_children[$offset]);
+ }
+
+ public function offsetGet($offset)
+ {
+ return isset($this->_children[$offset]) ? $this->_children[$offset] : null;
+ }
+
+ /**
+ * Renders the start tag
+ *
+ * @param string $tag_name
+ * @return string
+ */
+ public function render_start($tag_name = false)
+ {
+ $mapper = $this->_controller->get_mapper();
+ // render this for admin users only
+ if ($this->_controller->is_editable())
+ {
+ // add about
+ $this->set_attribute('about', $mapper->create_identifier($this->_controller->get_object()));
+ }
+
+ // add xml namespaces
+ foreach ($mapper->get_vocabularies() as $prefix => $uri)
+ {
+ $this->set_attribute('xmlns:' . $prefix, $uri);
+ }
+
+ return parent::render_start($tag_name);
+ }
+
+ public function render_content()
+ {
+ $ret = '';
+ foreach ($this->_children as $child)
+ {
+ $ret .= $child->render();
+ }
+ return $ret;
+ }
+}
+?>
66 config.php
View
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Config wrapper
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+class config
+{
+ /**
+ * The current schema
+ *
+ * @var string
+ */
+ protected $_schema;
+
+ protected $_defaults = array
+ (
+ 'vocabularies' => array(),
+ 'properties' => array(),
+ 'attributes' => array(),
+ 'storage' => false
+ );
+
+ protected $_property_defaults = array
+ (
+ 'type' => array('openpsa\createphp\propertyNode')
+ );
+
+ protected $_data = array();
+
+ public function __construct(array $data = array())
+ {
+ foreach ($data as $schema_name => $config)
+ {
+ if (empty($this->_schema))
+ {
+ $this->_schema = $schema_name;
+ }
+ foreach ($config['properties'] as $fieldname => $values)
+ {
+ $config['properties'][$fieldname] = array_merge($this->_property_defaults, $config['properties'][$fieldname]);
+ }
+ $this->_data[$schema_name] = array_merge($this->_defaults, $config);
+ }
+ }
+
+ public function set_schema($schema_name)
+ {
+ $this->_schema = $schema_name;
+ }
+
+ public function get($key)
+ {
+ return $this->_data[$this->_schema][$key];
+ }
+}
+?>
180 controller.php
View
@@ -0,0 +1,180 @@
+<?php
+/**
+ * Abstract baseclass for the CreateController
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+class controller extends node
+{
+ /**
+ * Flag that shows whether or not the object is editable
+ *
+ * @var boolean
+ */
+ private $_editable = true;
+
+ /**
+ * The mapper
+ *
+ * @var rdfMapper
+ */
+ protected $_mapper;
+
+ /**
+ * The current storage object, if any
+ *
+ * @var mixed
+ */
+ protected $_object;
+
+ /**
+ * Stores an array of rdf properties currently set for the controller
+ *
+ * @var array
+ */
+ protected $_properties = array();
+
+ /**
+ * The constructor
+ *
+ * @param rdfMapper $mapper
+ * @param controller $parent the parent controller for collection children
+ */
+ public function __construct(rdfMapper $mapper, controller $parent = null)
+ {
+ $this->_mapper = $mapper;
+ $this->_parent = $parent;
+ }
+
+ public function set_object($object, $schema_name)
+ {
+ $this->_object = $object;
+ $this->_properties = array();
+ $this->_mapper->get_config()->set_schema($schema_name);
+
+ foreach ($this->_mapper->get_config()->get('attributes') as $key => $value)
+ {
+ $this->set_attribute($key, $value);
+ }
+
+ $map = $this->_mapper->get_config()->get('properties');
+
+ // use rdf mapper to create element representations
+ foreach ($map as $fieldname => $config)
+ {
+ $classname = array_shift($config['type']);
+ $this->$fieldname = new $classname($config, $this);
+
+ if ($this->$fieldname instanceof propertyNode)
+ {
+ if (empty($config['rdf_name']))
+ {
+ $rdf_name = $this->_mapper->get_rdf_name($fieldname);
+ }
+ else
+ {
+ $rdf_name = $config['rdf_name'];
+ }
+ $this->$fieldname->set_attribute('property', $rdf_name);
+ $this->$fieldname->set_value($this->_mapper->get_property_value($object, $fieldname));
+ }
+ }
+
+ $this->set_editable($this->_mapper->is_editable($object));
+ }
+
+ public function get_object()
+ {
+ return $this->_object;
+ }
+
+ /**
+ * Magic getter
+ *
+ * @param string $key
+ * @return node
+ */
+ public function __get($key)
+ {
+ if (isset($this->_properties[$key]))
+ {
+ return $this->_properties[$key];
+ }
+ return null;
+ }
+
+ /**
+ * Magic setter
+ *
+ * @param string $key
+ * @param node $node
+ */
+ public function __set($key, node $node)
+ {
+ $this->_properties[$key] = $node;
+ }
+
+ /**
+ * Mapper getter
+ *
+ * @return rdfMapper
+ */
+ public function get_mapper()
+ {
+ return $this->_mapper;
+ }
+
+ public function set_editable($value)
+ {
+ $this->_editable = (bool) $value;
+ }
+
+ public function is_editable()
+ {
+ return $this->_editable;
+ }
+
+ /**
+ * Renders the start tag
+ *
+ * @param string $tag_name
+ * @return string
+ */
+ public function render_start($tag_name = false)
+ {
+ // render this for admin users only
+ if ($this->is_editable())
+ {
+ // add about
+ $this->set_attribute('about', $this->_mapper->create_identifier($this->_object));
+ }
+
+ // add xml namespaces
+ foreach ($this->_mapper->get_vocabularies() as $prefix => $uri)
+ {
+ $this->set_attribute('xmlns:' . $prefix, $uri);
+ }
+
+ return parent::render_start($tag_name);
+ }
+
+ public function render_content()
+ {
+ $output = '';
+ foreach ($this->_properties as $key => $prop)
+ {
+ $output .= $prop->render();
+ }
+ return $output;
+ }
+}
+?>
154 node.php
View
@@ -0,0 +1,154 @@
+<?php
+/**
+ * Encapsulates a node in the DOM tree
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+abstract class node
+{
+ /**
+ * HTML element to use
+ *
+ * @var string
+ */
+ protected $_tag_name = 'div';
+
+ /**
+ * The element's attributes
+ *
+ * @var array
+ */
+ protected $_attributes = array();
+
+ /**
+ * The element output template
+ *
+ * @var string
+ */
+ private $_template = "<__TAG_NAME__ __ATTRIBUTES__>__CONTENT__</__TAG_NAME__>\n";
+
+ /**
+ * The parent node
+ *
+ * @var node
+ */
+ protected $_parent;
+
+ /**
+ * The node's children, if any
+ *
+ * @var array
+ */
+ protected $_children = array();
+
+ /**
+ * Adds an additional attribute to the wrapper tag
+ *
+ * @param string $key
+ * @param string $value
+ */
+ public function set_attribute($key, $value)
+ {
+ $this->_attributes[$key] = $value;
+ }
+
+ /**
+ * Adds an additional attribute to the wrapper tag
+ *
+ * @param string $key
+ */
+ public function get_attribute($key)
+ {
+ return $this->_attributes[$key];
+ }
+
+ /**
+ * Sets the template
+ *
+ * @param string $template
+ */
+ public function set_template($template)
+ {
+ $this->_template = $template;
+ }
+
+ /**
+ * Renders the element
+ *
+ * @param string $tag_name
+ */
+ public function render_start($tag_name = false)
+ {
+ if (is_string($tag_name))
+ {
+ $this->_tag_name = $tag_name;
+ }
+
+ $template = explode('__CONTENT__', $this->_template);
+ $template = $template[0];
+
+ $replace = array
+ (
+ "__TAG_NAME__" => $this->_tag_name,
+ "__ATTRIBUTES__" => $this->render_attributes(),
+ );
+
+ return strtr($template, $replace);
+ }
+
+ /**
+ * Renders everything including wrapper html tag and properties
+ *
+ * @param string $tag_name
+ * @return string
+ */
+ public function render($tag_name = false)
+ {
+ $output = $this->render_start($tag_name);
+
+ $output .= $this->render_content();
+
+ $output .= $this->render_end();
+ return $output;
+ }
+
+ public function render_attributes()
+ {
+ // add additional attributes
+ $attributes = '';
+ foreach ($this->_attributes as $key => $value)
+ {
+ $attributes .= ' ' . $key . '="' . $value . '"';
+ }
+ return $attributes;
+ }
+
+ abstract function render_content();
+
+ public function render_end()
+ {
+ $template = explode('__CONTENT__', $this->_template);
+ $template = $template[1];
+
+ $replace = array
+ (
+ "__TAG_NAME__" => $this->_tag_name,
+ );
+ return strtr($template, $replace);
+ }
+
+ public function __toString()
+ {
+ return $this->render();
+ }
+}
+?>
70 propertyNode.php
View
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Encapsulates a property node in the DOM tree
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+class propertyNode extends node
+{
+ /**
+ * the element's content
+ *
+ * @var string
+ */
+ private $_value = '';
+
+ protected $_config;
+ protected $_controller;
+
+ public function __construct(array $config, controller $controller)
+ {
+ $this->_config = $config;
+ $this->_controller = $controller;
+ }
+
+ /**
+ * Sets the value
+ *
+ * @param string $value
+ */
+ public function set_value($value)
+ {
+ $this->_value = $value;
+ }
+
+ /**
+ * Value getter
+ *
+ * @return string
+ */
+ public function get_value()
+ {
+ return $this->_value;
+ }
+
+ public function render_content()
+ {
+ return $this->get_value();
+ }
+
+ public function render($tag_name = false)
+ {
+ // add rdf name for admin only
+ if (!$this->_controller->is_editable())
+ {
+ unset($this->_attributes['property']);
+ }
+
+ return parent::render($tag_name);
+ }
+}
+?>
155 rdfMapper.php
View
@@ -0,0 +1,155 @@
+<?php
+/**
+ * Abstract baseclass for rdfMapper
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+abstract class rdfMapper
+{
+ /**
+ * the config array containing the mappings
+ *
+ * @var config
+ */
+ protected $_config;
+
+ /**
+ * The vocabularies used
+ *
+ * @var array
+ */
+ protected $_vocabularies = array();
+
+ /**
+ * Constructor
+ *
+ * @param config $config
+ */
+ public function __construct(config $config = null)
+ {
+ if (null === $config)
+ {
+ $config = new config;
+ }
+ $this->set_config($config);
+ }
+
+ /**
+ * Config getter
+ *
+ * @return array
+ */
+ public function get_config()
+ {
+ return $this->_config;
+ }
+
+ /**
+ * Config setter
+ *
+ * @param config $config
+ */
+ public function set_config(config $config)
+ {
+ $this->_config = $config;
+ }
+
+ /**
+ * Register new namespace
+ *
+ * @param string $prefix
+ * @param string $uri
+ */
+ public function register_vocabulary($prefix, $uri)
+ {
+ $this->_vocabularies[$prefix] = $uri;
+ }
+
+ /**
+ * Get all namespaces
+ *
+ * @return array
+ */
+ public function get_vocabularies()
+ {
+ foreach ($this->_config->get('vocabularies') as $prefix => $uri)
+ {
+ $this->register_vocabulary($prefix, $uri);
+ }
+ return $this->_vocabularies;
+ }
+
+ /**
+ * Set object property
+ *
+ * @param mixed $key
+ * @param mixed $object
+ * @param mixed $value
+ * @return mixed
+ */
+ abstract function set_property_value($object, $key, $value);
+
+ /**
+ * Get object property
+ *
+ * @param mixed $key
+ * @param mixed $object
+ * @return mixed
+ */
+ abstract function get_property_value($object, $key);
+
+ abstract function get_rdf_name($fieldname);
+
+ abstract function is_editable($object);
+
+ /**
+ * Get object's children
+ *
+ * @param mixed $object
+ * @param array $config
+ * @return array
+ */
+ abstract function get_children($object, config $config);
+
+ abstract function prepare_object(config $config);
+
+ /**
+ * Save object
+ *
+ * @param mixed $object
+ */
+ abstract function store($object);
+
+ /**
+ * Load object by identifier
+ *
+ * @param string $identifier
+ * @return mixed The storage object or false if nothing is found
+ */
+ abstract function get_by_identifier($identifier);
+
+ /**
+ * Create identifier for passed object
+ *
+ * @param mixed $object
+ * @return string
+ */
+ abstract function create_identifier($object);
+
+ /**
+ * Delete an object
+ *
+ * @param mixed $object
+ */
+ abstract function delete($object);
+}
+?>
208 restservice.php
View
@@ -0,0 +1,208 @@
+<?php
+/**
+ * REST service backend
+ *
+ * @copyright CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @author CONTENT CONTROL GbR, http://www.contentcontrol-berlin.de
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @package openpsa.createphp
+ */
+
+namespace openpsa\createphp;
+
+/**
+ * @package openpsa.createphp
+ */
+class restservice
+{
+ /**
+ * The mapper object
+ *
+ * @var rdfMapper
+ */
+ protected $_mapper;
+
+ /**
+ * The operation verb
+ *
+ * @var string
+ */
+ protected $_verb;
+
+ /**
+ * The passed data, if any
+ *
+ * @var array
+ */
+ protected $_data;
+
+ /**
+ * The constructor
+ *
+ * @param rdfMapper $mapper
+ */
+ public function __construct(rdfMapper $mapper, array $data = null)
+ {
+ $this->_data = $data;
+ $this->_verb = strtolower($_SERVER['REQUEST_METHOD']);
+ if (null !== $mapper)
+ {
+ $this->set_mapper($mapper);
+ }
+ }
+
+ public function get_data()
+ {
+ return $this->_data;
+ }
+
+ /**
+ * Get transmitted properties
+ *
+ * @return array
+ */
+ private function _get_properties()
+ {
+ $return = array();
+
+ foreach ($this->_data as $key => $value)
+ {
+ if (substr($key, 0, 1) === '@')
+ {
+ continue;
+ }
+ $key = trim($key, '<>');
+ $return[$key] = $value;
+ }
+ return $return;
+ }
+
+ /**
+ * Mapper setter
+ *
+ * @param rdfMapper $mapper
+ */
+ public function set_mapper(rdfMapper $mapper)
+ {
+ $this->_mapper = $mapper;
+ }
+
+ /**
+ * Mapper getter
+ *
+ * @return rdfMapper
+ */
+ public function get_mapper()
+ {
+ return $this->_mapper;
+ }
+
+ /**
+ * Run the service
+ */
+ public function run()
+ {
+ switch ($this->_verb)
+ {
+ case 'get':
+ // do not handle get
+ break;
+ case 'delete':
+ $this->_handle_delete();
+ break;
+ case 'post':
+ $this->_handle_create();
+ break;
+ case 'put':
+ $this->_handle_update();
+ break;
+ }
+ }
+
+ /**
+ * Handle post request
+ */
+ private function _handle_create()
+ {
+ $map = $this->_mapper->get_config()->get('properties');
+ $received_data = $this->_get_properties();
+
+ $parent = null;
+ foreach ($map as $fieldname => $config)
+ {
+ if (!isset($config['attributes']['rev']))
+ {
+ continue;
+ }
+
+ $parentfield = $this->_expand_property_name($config['attributes']['rev']);
+ if (!empty($received_data[$parentfield]))
+ {
+ $parent_identifier = trim($received_data[$parentfield][0], '<>');
+ $parent = $this->_mapper->get_by_identifier($parent_identifier);
+ $this->_mapper->get_config()->set_schema($config['type'][1]);
+ }
+ }
+
+ $object = $this->_mapper->prepare_object($this->_mapper->get_config(), $parent);
+ return $this->_store_data($object);
+ }
+
+ /**
+ * Handle put request
+ */
+ private function _handle_update()
+ {
+ $object = $this->_mapper->get_by_identifier(trim($this->_data['@subject'], '<>'));
+ return $this->_store_data($object);
+ }
+
+ private function _store_data($object)
+ {
+ $new_values = $this->_get_properties();
+
+ $properties = $this->_mapper->get_config()->get('properties');
+
+ foreach ($properties as $fieldname => $config)
+ {
+ //TODO: Figure out a proper way to do this
+ if (!empty($config['attributes']['rel']))
+ {
+ continue;
+ }
+ if (empty($config['rdf_name']))
+ {
+ $rdf_name = $this->_mapper->get_rdf_name($fieldname);
+ }
+ else
+ {
+ $rdf_name = $config['rdf_name'];
+ }
+ $expanded_name = $this->_expand_property_name($rdf_name);
+
+ if (array_key_exists($expanded_name, $new_values))
+ {
+ $object = $this->_mapper->set_property_value($object, $fieldname, $new_values[$expanded_name]);
+ }
+ }
+
+ return $this->_mapper->store($object);
+ }
+
+ private function _expand_property_name($name)
+ {
+ $name = explode(":", $name);
+ $vocabularies = $this->_mapper->get_vocabularies();
+ return $vocabularies[$name[0]] . $name[1];
+ }
+
+ /**
+ * Handle delete request
+ */
+ private function _handle_delete()
+ {
+ $object = $this->_mapper->get_by_identifier($_REQUEST["uri"]);
+ return $this->_mapper->delete($object);
+ }
+}
+?>

No commit comments for this range

Something went wrong with that request. Please try again.