SolrService::coreCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Services;
4
5
use Apache_Solr_Response;
6
use SilverStripe\Core\Config\Config;
7
8
/**
9
 * The API for accessing the primary Solr installation, which includes both SolrService_Core,
10
 * plus extra methods for interrogating, creating, reloading and getting SolrService_Core instances
11
 * for Solr cores.
12
 */
13
class SolrService extends SolrService_Core
14
{
15
    private static $core_class = SolrService_Core::class;
16
17
    /**
18
     * Handle encoding the GET parameters and making the HTTP call to execute a core command
19
     *
20
     * @param string $command
21
     * @param string $core
22
     * @param array $params
23
     * @return Apache_Solr_Response
24
     */
25
    protected function coreCommand($command, $core, $params = array())
26
    {
27
        $command = strtoupper($command);
28
        $params = array_merge($params, array('action' => $command, 'wt' => 'json'));
29
        $params[$command === 'CREATE' ? 'name' : 'core'] = $core;
30
31
        return $this->_sendRawGet($this->_constructUrl('admin/cores', $params));
32
    }
33
34
    /**
35
     * Is the passed core active?
36
     *
37
     * @param string $core The name of the core (an encoded class name)
38
     * @return boolean True if that core exists & is active
39
     */
40
    public function coreIsActive($core)
41
    {
42
        // Request the status of the full core name
43
        $result = $this->coreCommand('STATUS', $core);
44
        return isset($result->status->$core->uptime);
45
    }
46
47
    /**
48
     * Create a new core
49
     *
50
     * @param string $core The name of the core
51
     * @param string $instancedir The base path of the core on the server
52
     * @param string $config The filename of solrconfig.xml on the server. Default is $instancedir/solrconfig.xml
53
     * @param string $schema The filename of schema.xml on the server. Default is $instancedir/schema.xml
54
     * @param string $datadir The path to store data for this core on the server. Default depends on solrconfig.xml
55
     * @return Apache_Solr_Response
56
     */
57
    public function coreCreate($core, $instancedir, $config = null, $schema = null, $datadir = null)
58
    {
59
        $args = array('instanceDir' => $instancedir);
60
        if ($config) {
61
            $args['config'] = $config;
62
        }
63
        if ($schema) {
64
            $args['schema'] = $schema;
65
        }
66
        if ($datadir) {
67
            $args['dataDir'] = $datadir;
68
        }
69
70
        return $this->coreCommand('CREATE', $core, $args);
71
    }
72
73
    /**
74
     * Reload a core
75
     *
76
     * @param string $core The name of the core
77
     * @return Apache_Solr_Response
78
     */
79
    public function coreReload($core)
80
    {
81
        return $this->coreCommand('RELOAD', $core);
82
    }
83
84
    /**
85
     * Create a new Solr4Service_Core instance for the passed core
86
     *
87
     * @param string $core The name of the core
88
     * @return Solr4Service_Core
89
     */
90
    public function serviceForCore($core)
91
    {
92
        $klass = Config::inst()->get(get_called_class(), 'core_class');
93
        return new $klass($this->_host, $this->_port, $this->_path . $core, $this->_httpTransport);
94
    }
95
}
96