Skip to content

Commit

Permalink
implement repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Semm committed Jan 30, 2012
1 parent ecc47e0 commit 0a2caba
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 29 deletions.
20 changes: 20 additions & 0 deletions Query/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ abstract class AbstractQuery {
*/
protected $solrQuery = null;

/**
*
* @var object
*/
private $entity = null;

public function __construct() {
$this->solrQuery = new \SolrQuery('*:*');
}

/**
* @return the $entity
*/
public function getEntity() {
return $this->entity;
}

/**
* @param object $entity
*/
public function setEntity($entity) {
$this->entity = $entity;
}

/**
* @return \SolrQuery
*/
Expand Down
30 changes: 30 additions & 0 deletions Query/FindByDocumentNameQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace FS\SolrBundle\Query;

class FindByDocumentNameQuery extends AbstractQuery {
/**
*
* @var \SolrInputDocument
*/
private $document = null;

public function __construct(\SolrInputDocument $document) {
parent::__construct();

$this->document = $document;
}

public function getQueryString() {
$documentNameField = $this->document->getField('document_name_s');

if ($documentNameField == null) {
throw new \RuntimeException('documentName should not be null');
}

$query = 'document_name_s:'.$documentNameField->values[0];

return $query;
}
}

?>
8 changes: 8 additions & 0 deletions Query/FindByIdentifierQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function getQueryString() {
$idField = $this->document->getField('id');
$documentNameField = $this->document->getField('document_name_s');

if ($idField == null) {
throw new \RuntimeException('id should not be null');
}

if ($documentNameField == null) {
throw new \RuntimeException('documentName should not be null');
}

$query = 'id:'.$idField->values[0].' AND document_name_s:'.$documentNameField->values[0];

return $query;
Expand Down
20 changes: 1 addition & 19 deletions Query/SolrQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class SolrQuery extends AbstractQuery {

private $strict = false;

/**
*
* @var object
*/
private $entity = null;


/**
*
Expand Down Expand Up @@ -77,20 +73,6 @@ public function addSearchTerm($field, $value) {
return $this;
}

/**
* @return the $entity
*/
public function getEntity() {
return $this->entity;
}

/**
* @param object $entity
*/
public function setEntity($entity) {
$this->entity = $entity;
}

public function addField($field) {
$documentFieldsAsValues = array_flip($this->mappedFields);
if (array_key_exists($field, $documentFieldsAsValues)) {
Expand Down
8 changes: 0 additions & 8 deletions Repository/EntityRepository.php

This file was deleted.

95 changes: 95 additions & 0 deletions Repository/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
namespace FS\SolrBundle\Repository;

use FS\SolrBundle\Query\FindByDocumentNameQuery;

use FS\SolrBundle\Query\FindByIdentifierQuery;

use FS\SolrBundle\SolrFacade;

class Repository implements RepositoryInterface {

/**
*
* @var SolrFacade
*/
private $solr = null;

/**
*
* @var object
*/
private $entity = null;

public function __construct(SolrFacade $solr, $entity) {
$this->solr = $solr;

$this->entity = $entity;
}

/* (non-PHPdoc)
* @see FS\SolrBundle\Repository.RepositoryInterface::find()
*/
public function find($id) {
$this->entity->setId($id);

$mapper = $this->solr->getMapper();
$mapper->setMappingCommand($this->solr->getCommandFactory()->get('all'));
$document = $mapper->toDocument($this->entity);

$query = new FindByIdentifierQuery($document);
$query->setEntity($this->entity);
$found = $this->solr->query($query);

if (count($found) == 0) {
return null;
}

return array_pop($found);
}

/* (non-PHPdoc)
* @see FS\SolrBundle\Repository.RepositoryInterface::findAll()
*/
public function findAll() {
$mapper = $this->solr->getMapper();
$mapper->setMappingCommand($this->solr->getCommandFactory()->get('all'));

$document = $mapper->toDocument($this->entity);

if (null === $document) {
return null;
}

$document->deleteField('id');

$query = new FindByDocumentNameQuery($document);
$query->setEntity($this->entity);

return $this->solr->query($query);
}

/* (non-PHPdoc)
* @see FS\SolrBundle\Repository.RepositoryInterface::findBy()
*/
public function findBy(array $args) {
$query = $this->solr->createQuery($this->entity);

foreach ($args as $fieldName => $fieldValue) {
$query->addSearchTerm($fieldName, $fieldValue);
}

return $this->solr->query($query);
}

/* (non-PHPdoc)
* @see FS\SolrBundle\Repository.RepositoryInterface::findOneBy()
*/
public function findOneBy(array $args) {
$found = $this->findBy($args);

return array_pop($found);
}
}

?>
14 changes: 14 additions & 0 deletions Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace FS\SolrBundle\Repository;

interface RepositoryInterface {
public function findBy(array $args);

public function find($id);

public function findOneBy(array $args);

public function findAll();
}

?>
37 changes: 35 additions & 2 deletions SolrFacade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
namespace FS\SolrBundle;

use FS\SolrBundle\Query\AbstractQuery;

use FS\SolrBundle\Repository\Repository;

use Doctrine\ORM\Configuration;

use FS\SolrBundle\Query\SolrQuery;
Expand Down Expand Up @@ -51,10 +55,28 @@ public function __construct(SolrConnection $connection, CommandFactory $commandF
$this->entityMapper = new EntityMapper();
}

/**
* @return EntityMapper
*/
public function getMapper() {
return $this->entityMapper;
}

/**
* @return CommandFactory
*/
public function getCommandFactory() {
return $this->commandFactory;
}

public function setDoctrineConfiguration(Configuration $doctrineConfiguration) {
$this->doctrineConfiguration = $doctrineConfiguration;
}

/**
*
* @param SolrQuery $entity
*/
public function createQuery($entity) {
$class = $this->getClass($entity);
$entity = new $class;
Expand All @@ -68,8 +90,19 @@ public function createQuery($entity) {
return $query;
}

/**
*
* @param RepositoryInterface $entity
*/
public function getRepository($entityAlias) {
$class = $this->getClass($entityAlias);
$entity = new $class;

return new Repository($this, $entity);
}

private function getClass($entity) {
if (class_exists($entity)) {
if (is_object($entity) || class_exists($entity)) {
return $entity;
}

Expand Down Expand Up @@ -136,7 +169,7 @@ private function addDocumentToIndex($entity) {
*
* @return array
*/
public function query(SolrQuery $query) {
public function query(AbstractQuery $query) {
$solrQuery = $query->getSolrQuery();

try {
Expand Down
Loading

0 comments on commit 0a2caba

Please sign in to comment.