Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ergnuor committed Apr 20, 2020
1 parent 6c0d312 commit 3667956
Show file tree
Hide file tree
Showing 65 changed files with 2,778 additions and 2,183 deletions.
32 changes: 9 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ And `/path/to/sphinx/config/preciousConfig.conf` file should contents something
// generated by composer --dump-autoload
require_once '/path/to/autoload/autoload.php';

use Ergnuor\SphinxConfig\SphinxConfig;
use Ergnuor\SphinxConfig\Config;

$config = new SphinxConfig();
$config
->setSrcPath('/path/to/config/source')
->make('preciousConfig');
$config = Config::phpArrayToNativeConfigStdout('/path/to/config/source');
$config ->transform('preciousConfig');
```

### Application integrated
Expand All @@ -30,14 +28,13 @@ So script may looks like this:

//...

use Ergnuor\SphinxConfig\SphinxConfig;
use Ergnuor\SphinxConfig\Config;

$config = new SphinxConfig();
$config
->setSrcPath('/path/to/config/source')
// We need to set path where Sphinx configuration file will be generated
->setDstPath('/path/to/sphinx/config')
->make('preciousConfig');
$config = Config::phpArrayToNativeConfigFile(
'/path/to/config/source',
'/path/to/sphinx/config'
);
$config->transform('preciousConfig');

exec('searchd -c /path/to/sphinnx/config/presiousConfig.conf');

Expand Down Expand Up @@ -254,17 +251,6 @@ Multiline values are automatically padded with a trailing slash. This is useful
## Custom parameters
Parameter will be ignored if its name starts with an underscore. This allows you to add parameters for internal use, for example, if the configuration is being pre-processed and you want to be able to configure this process.

## Method list
- **```setSrcPath($srcPath)```**
Sets the directory containing source configurations,
- **```setDstPath($dstPath)```**
Sets the directory where resulting Sphinx configuration will be created,
- **```make($configName)```**
Generates Sphinx configuration in the `dstPath` directory or sends it into STDOUT if no `dstPath` specified,
- **```setDefaultSrcPath($srcPath)```**
Sets the default directory containing source configurations. Applies to all instances of SphinxConfig,
- **```setDefaultDstPath($dstPath)```**
Sets the default directory where resulting Sphinx configuration will be created. Applies to all instances of SphinxConfig,
## Configuration parameters list
- **```'extends'```**
Specifies the name of the parent block. Can refer to a block from another configuration located in the same directory. For that you must specify the configuration name separated from the block name with `@` symbol.
Expand Down
93 changes: 93 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Ergnuor\SphinxConfig;

class Config
{
/**
* @var array
*/
private $placeholderValues = [];

/**
* @var Section\Reader\Adapter
*/
private $sectionReaderAdapter;

/**
* @var Section\Writer\Adapter
*/
private $sectionWriterAdapter;

/**
* @param Section\Reader\Adapter $sectionReaderAdapter
* @param Section\Writer\Adapter $sectionWriterAdapter
*/
public function __construct(
Section\Reader\Adapter $sectionReaderAdapter,
Section\Writer\Adapter $sectionWriterAdapter
)
{
$this->sectionReaderAdapter = $sectionReaderAdapter;
$this->sectionWriterAdapter = $sectionWriterAdapter;
}

public static function phpArrayToNativeConfigStdout($srcPath)
{
return new static(
new Section\Reader\Adapter\File\PhpArray($srcPath),
new Section\Writer\Adapter\NativeConfig()
);
}

public static function phpArrayToNativeConfigFile($srcPath, $dstPath)
{
return new static(
new Section\Reader\Adapter\File\PhpArray($srcPath),
new Section\Writer\Adapter\NativeConfig($dstPath)
);
}

/**
* @param string $configName
* @return $this
*/
public function transform($configName)
{
$sections = Section\Type::getTypes();

$this->sectionWriterAdapter->reset();
$this->sectionReaderAdapter->reset();

foreach ($sections as $sectionName) {
$section = new Section(
$configName,
$sectionName,
$this->sectionReaderAdapter,
$this->sectionWriterAdapter,
$this->getPlaceholderValues()
);
$section->transform();
}

return $this;
}

/**
* @return array
*/
public function getPlaceholderValues()
{
return $this->placeholderValues;
}

/**
* @param array $placeholderValues
* @return Config
*/
public function setPlaceholderValues(array $placeholderValues)
{
$this->placeholderValues = $placeholderValues;
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Ergnuor\SphinxConfig\Exception;

class SourceException extends Exception
class ReaderException extends Exception
{

}
Loading

0 comments on commit 3667956

Please sign in to comment.