Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
TASK: Migrate existing code (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSiepmann authored Dec 9, 2016
1 parent d89e616 commit b079dd8
Show file tree
Hide file tree
Showing 28 changed files with 1,355 additions and 30 deletions.
15 changes: 15 additions & 0 deletions Classes/Command/IndexCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@
* 02110-1301, USA.
*/

use Leonmrni\SearchCore\Domain\Index\IndexerFactory;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;

/**
* Command controller to provide indexing through CLI.
*/
class IndexCommandController extends CommandController
{
/**
* @var IndexerFactory
*/
protected $indexerFactory;

/**
* @param IndexerFactory $factory
*/
public function injectIndexerFactory(IndexerFactory $factory)
{
$this->indexerFactory = $factory;
}

/**
* Will index the given table or everything.
*
Expand All @@ -36,5 +50,6 @@ public function indexCommand($table)
{
// TODO: Allow to index multiple tables at once?
// TODO: Also allow to index everything?
$this->indexerFactory->getIndexer($table)->index();
}
}
75 changes: 75 additions & 0 deletions Classes/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Leonmrni\SearchCore\Connection;

/*
* Copyright (C) 2016 Daniel Siepmann <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/**
* Defines interface for connections to storage backend for interacting with documents.
*/
interface ConnectionInterface
{
/**
* Will add a new document, based on his identifier and record type.
*
* @param string $recordType
* @param int $identifier
* @param array $record
*
* @return
*/
public function add($recordType, $identifier, array $record);

/**
* Add the given records.
*
* @param string $recordType
* @param array $records
*/
public function addDocuments($recordType, array $records);

/**
* Will update an existing document, based on his identifier and record type.
*
* @param string $recordType
* @param int $identifier
* @param array $record
*
* @return
*/
public function update($recordType, $identifier, array $record);

/**
* Will remove an existing document, based on his identifier and record type.
*
* @param string $recordType
* @param int $identifier
*
* @return
*/
public function delete($recordType, $identifier);

/**
* Search by given request and return result.
*
* @param SearchRequestInterface $searchRequest
* @return SearchResultInterface
*/
public function search(SearchRequestInterface $searchRequest);
}
140 changes: 140 additions & 0 deletions Classes/Connection/Elasticsearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
namespace Leonmrni\SearchCore\Connection;

/*
* Copyright (C) 2016 Daniel Siepmann <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

use TYPO3\CMS\Core\SingletonInterface as Singleton;

/**
* Outer wrapper to elasticsearch.
*/
class Elasticsearch implements Singleton, ConnectionInterface
{
/**
* @var Elasticsearch\Connection
*/
protected $connection;

/**
* @var IndexFactory
*/
protected $indexFactory;

/**
* @var TypeFactory
*/
protected $typeFactory;

/**
* @var DocumentFactory
*/
protected $documentFactory;

/**
* @var \TYPO3\CMS\Core\Log\Logger
*/
protected $logger;

/**
* Inject log manager to get concrete logger from it.
*
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
*/
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager)
{
$this->logger = $logManager->getLogger(__CLASS__);
}

/**
* @param Elasticsearch\Connection $connection
* @param Elasticsearch\IndexFactory $indexFactory
* @param Elasticsearch\TypeFactory $typeFactory
* @param Elasticsearch\DocumentFactory $documentFactory
*/
public function __construct(
Elasticsearch\Connection $connection,
Elasticsearch\IndexFactory $indexFactory,
Elasticsearch\TypeFactory $typeFactory,
Elasticsearch\DocumentFactory $documentFactory
) {
$this->connection = $connection;
$this->indexFactory = $indexFactory;
$this->typeFactory = $typeFactory;
$this->documentFactory = $documentFactory;
}

public function add($recordType, $identifier, array $record)
{
throw new \Exception('Implement', 1481190734);
}

public function delete($recordType, $identifier)
{
throw new \Exception('Implement', 1481190734);
}

public function update($tableName, $identifier, array $record)
{
$this->addDocument($tableName, $identifier, $record);
}

protected function addDocument($tableName, $identifier, array $record)
{
throw new \Exception('Implement', 1481192791);
}

/**
* Add the given records to elasticsearch.
*
* @param string $tableName
* @param array $records
*/
public function addDocuments($tableName, array $records)
{
$type = $this->typeFactory->getType(
$this->indexFactory->getIndex(
$this->connection,
$tableName
),
$tableName
);

$type->addDocuments(
$this->documentFactory->getDocuments($tableName, $records)
);

$type->getIndex()->refresh();
}

/**
* @param SearchRequestInterface $searchRequest
* @return SearchResultInterface
*/
public function search(SearchRequestInterface $searchRequest)
{
$this->logger->debug('Search for', [$searchRequest->getSearchTerm()]);

$search = new \Elastica\Search($this->connection->getClient());
$search->addIndex('typo3content');

// TODO: Return wrapped result to implement our interface.
return $search->search($searchRequest->getSearchTerm());
}
}
95 changes: 95 additions & 0 deletions Classes/Connection/Elasticsearch/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
namespace Leonmrni\SearchCore\Connection\Elasticsearch;

/*
* Copyright (C) 2016 Daniel Siepmann <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

use TYPO3\CMS\Core\SingletonInterface as Singleton;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

/**
* The current connection to elasticsearch.
*
* Wrapper for Elastica\Client.
*/
class Connection implements Singleton
{
/**
* @var \Elastica\Client
*/
protected $elasticaClient;

/**
* @var array
*/
protected $settings;

/**
* @param \Elastica\Client $elasticaClient
*/
public function __construct(\Elastica\Client $elasticaClient = null)
{
$this->elasticaClient = $elasticaClient;
}

/**
* Inject news settings via ConfigurationManager.
*
* TODO: Refactor to configuration object to have a singleton holding the
* settings with validation and propper getter?
*
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
*/
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
{
$this->settings = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'SearchCore',
'search'
);
}

/**
* Used to configure elasticaClient if no one was injected. Will use
* injected settings for configuration.
*/
public function initializeObject()
{
if ($this->elasticaClient === null) {
$this->elasticaClient = new \Elastica\Client([
'host' => $this->settings['host'],
'port' => $this->settings['port'],
// TODO: Make configurable
// 'log' => 'file',
]);
// TODO: Make configurable.
// new \Elastica\Log($this->elasticaClient);
}
}

/**
* Get the concrete client for internal usage!
*
* @return \Elastica\Client
*/
public function getClient()
{
return $this->elasticaClient;
}
}
Loading

0 comments on commit b079dd8

Please sign in to comment.