Created
October 9, 2010 22:42
-
-
Save beberlei/618690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Symfony\CMF\Frontend; | |
use PHPCR\SessionInterface; | |
class ContentManager | |
{ | |
private $session; | |
public function __construct(SessionInterface $session) | |
{ | |
$this->session = $session; | |
$this->unitOfWork = new UnitOfWork($this); | |
} | |
public function getSession() | |
{ | |
return $this->session; | |
} | |
public function persist($content) | |
{ | |
$this->unitOfWork->persist($content); | |
} | |
public function remove($content) | |
{ | |
$this->unitOfWork->remove($content); | |
} | |
public function flush() | |
{ | |
$this->unitOfWork->commit(); | |
} | |
public function find($className, $path) | |
{ | |
return $this->getRepository($className)->find($path); | |
} | |
public function findByUUID($className, $uuid) | |
{ | |
return $this->getRepository($className)->findByUUID($path); | |
} | |
public function executeQuery(PHPCR_Query_QueryInterface $query); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$phpcrSession = $this->getContainer()->getService('phpcr.session'); | |
$jcrManager = $this->getContainer()->getService('symfony.cmf.objectmanager'); | |
$path = "/homepage/blogposts/post" . $postId; | |
$blogPost = $jcrManager->find("Application\MyBundle\Entities\BlogPost", $path); | |
$blogPost = $jcrManager->findByUUID("Application\MyBundle\Entities\BlogPost", $uuid); | |
$query = $phpcrSession.... (create query); | |
$blogPosts = $jcrManager->executeQuery($query); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spontaneous thoughts: | |
1. Symfony Mapper | |
What just hits me, JCR only returns Nodes - ever. So we have to transform those nodes | |
into domain objects. JCR has Metadata and each node has a node-type (for example BlogPost). | |
There are some default node types (See section 3.7.10), Symfony CMF JCRMapper (Prototypename) | |
would need to provide "domain object" implementations for these. HOWEVER, JCR strongly | |
supports the concept of Mixins, which we cannot nativly re-implement in PHP (no multiple inheritance). | |
Each node has a primary type that cannot be changed after instantiation. This would | |
be for example an "BlogPost", or "Article". These have required and optional properties. | |
Required properties can be mapped <-> to fields of the Domain object. | |
1. Say we have a BlogPost node with the properties "title", "text". The following | |
mapping would have to be done: | |
class Node | |
{ | |
private $identifier; | |
private $path; | |
private $properties = array( | |
"title" => "", | |
"text" => "", | |
); | |
} | |
to: | |
/** @jcr:node(type="BlogPost") */ | |
class BlogPost | |
{ | |
/** @jcr:identifier */ | |
public $id; | |
/** @jcr:path */ | |
public $path; | |
/** @jcr:property("title") */ | |
public $title; | |
/** @jcr:property("text") */ | |
public $text; | |
} | |
2. Say now we add the mixin "mix:created" and "mix:lastModified" to the BlogPost type. | |
We would have new properties: | |
class Node | |
{ | |
private $properties = array( | |
"title" => "", | |
"text" => "", | |
"jcr:created" => "", | |
"jcr:lastModified" => "", | |
); | |
} | |
to: | |
/** @jcr:node(type="BlogPost") */ | |
class BlogPost | |
{ | |
/** @jcr:property("title") */ | |
public $title; | |
/** @jcr:property("text") */ | |
public $text; | |
/** @jcr:property("jcr:created") */ | |
public $created; | |
/** @jcr:property("jcr:lastModified") */ | |
public $modified; | |
} | |
3. Say now we have optional or additional properties that cannot be known at programming | |
time. For example we allow the users of our website to add arbitrary fields to the "BlogPost" | |
type and someone comes up with "url-i-copy-pasted-this-from". | |
class Node | |
{ | |
private $properties = array( | |
"title" => "", | |
"text" => "", | |
"jcr:created" => "", | |
"jcr:lastModified" => "", | |
"url-i-copy-pasted-this-from" => "", | |
); | |
} | |
/** @jcr:node(type="BlogPost") */ | |
class BlogPost | |
{ | |
/** @jcr:property("title") */ | |
public $title; | |
/** @jcr:property("text") */ | |
public $text; | |
/** @jcr:property("jcr:created") */ | |
public $created; | |
/** @jcr:property("jcr:lastModified") */ | |
public $modified; | |
/** @jcr:dynamic-properties */ | |
public $additionalProperties = array( | |
"url-i-copy-pasted-this-from" => "", | |
); | |
} | |
4. Now I want to add optional child node type "Comment" to the BlogPost. This | |
would make use of the "name-pattern" traversal of child nodes. | |
class Node | |
{ | |
private $properties = array( | |
"title" => "", | |
"text" => "", | |
"jcr:created" => "", | |
"jcr:lastModified" => "", | |
"url-i-copy-pasted-this-from" => "", | |
); | |
private $childNodes = array(...); | |
} | |
/** @jcr:node(type="BlogPost") */ | |
class BlogPost | |
{ | |
/** @jcr:property("title") */ | |
public $title; | |
/** @jcr:property("text") */ | |
public $text; | |
/** @jcr:property("jcr:created") */ | |
public $created; | |
/** @jcr:property("jcr:lastModified") */ | |
public $modified; | |
/** @jcr:dynamic-properties */ | |
public $additionalProperties = array( | |
"url-i-copy-pasted-this-from" => "", | |
); | |
/** | |
* Using the traversal API defined in 5.2, $node->getNodes("pattern") and | |
* should be hidden behind a lazy-load proxy that references the Session and Node. | |
* | |
* @jcr:children("name-pattern") | |
*/ | |
public $comments; | |
} | |
5. Now we also want to have a node-to-one reference to a teaser image. We use | |
the property-type "path" for this called "teaser-image": | |
class Node | |
{ | |
private $properties = array( | |
"title" => "", | |
"text" => "", | |
"jcr:created" => "", | |
"jcr:lastModified" => "", | |
"url-i-copy-pasted-this-from" => "", | |
"teaser-image" => "", | |
); | |
private $childNodes = array(...); | |
} | |
and the domain object would be configured such: | |
/** @jcr:node(type="BlogPost") */ | |
class BlogPost | |
{ | |
// ... | |
/** | |
* JCR knows this is a property-type "path" and inserts a proxy | |
* for the nodeType that is on the other end of the reference. | |
* | |
* @jcr:property("teaser-image", nodeType="File") | |
*/ | |
public $teaserImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment