Passed
Push — master ( b4e3f0...d6a119 )
by Robbie
04:16 queued 02:31
created

src/Solr/Solr.php (3 issues)

1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Core\Manifest\Module;
8
use SilverStripe\Core\Manifest\ModuleLoader;
9
use SilverStripe\FullTextSearch\Search\FullTextSearch;
10
use SilverStripe\FullTextSearch\Solr\Services\Solr4Service;
11
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service;
12
13
class Solr
14
{
15
    /**
16
     * Configuration on where to find the solr server and how to get new index configurations into it.
17
     *
18
     * Required fields:
19
     * host (default: localhost) - The host or IP Solr is listening on
20
     * port (default: 8983) - The port Solr is listening on
21
     * path (default: /solr) - The suburl the solr service is available on
22
     *
23
     * Optional fields:
24
     * version (default: 4) - The Solr server version. Currently supports 3 and 4 (you can add a sub-version like 4.5 if
25
     *   you like, but currently it has no effect)
26
     * service (default: depends on version, Solr3Service for 3, Solr4Service for 4)
27
     *   the class that provides actual communcation to the Solr server
28
     * extraspath (default: <basefolder>/fulltextsearch/conf/solr/{version}/extras/) - Absolute path to
29
     *   the folder containing templates which are used for generating the schema and field definitions.
30
     * templates (default: <basefolder>/fulltextsearch/conf/solr/{version}/templates/) - Absolute path to
31
     *   the configuration default files, e.g. solrconfig.xml.
32
     *
33
     * indexstore => an array with
34
     *
35
     *   mode - a classname which implements SolrConfigStore, or 'file', 'webdav' or 'post'
36
     *
37
     *   When mode == SolrConfigStore_File or 'file' (indexes should be written on a local filesystem)
38
     *      path - The (locally accessible) path to write the index configurations to.
39
     *      remotepath (default: the same as indexpath) - The path that the Solr server will read the index configurations from
40
     *
41
     *   When mode == SolrConfigStore_Post or 'post' (indexes should stored on a remote Solr server via post)
42
     *   This mode will require custom software on the remote solr server which handles receiving the post and
43
     *   passing on that information to solr. It is up to the user of this mode to write such software.
44
     *      path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations
45
     *      port (default: none) - The port on the remote server which is set up to receive the post information
46
     *
47
     *   When mode == SolrConfigStore_WebDAV or 'webdav' (indexes should stored on a remote Solr server via webdav)
48
     *      auth (default: none) - A username:password pair string to use to auth against the webdav server
49
     *      path (default: /solrindex) - The suburl on the solr host that is set up to accept index configurations via webdav
50
     *      port (default: none) - The port for WebDAV if different from the Solr port
51
     *      remotepath - The path that the Solr server will read the index configurations from
52
     */
53
    protected static $solr_options = array();
54
55
    /** A cache of solr_options with the defaults all merged in */
56
    protected static $merged_solr_options = null;
57
58
    /**
59
     * Update the configuration for Solr. See $solr_options for a discussion of the accepted array keys
60
     * @param array $options - The options to update
61
     */
62
    public static function configure_server($options = array())
63
    {
64
        self::$solr_options = array_merge(self::$solr_options, $options);
65
        self::$merged_solr_options = null;
66
67
        self::$service_singleton = null;
68
        self::$service_core_singletons = array();
69
    }
70
71
    /**
72
     * Get the configured Solr options with the defaults all merged in
73
     * @return array - The merged options
74
     */
75
    public static function solr_options()
76
    {
77
        if (self::$merged_solr_options) {
78
            return self::$merged_solr_options;
79
        }
80
81
        $defaults = array(
82
            'host' => 'localhost',
83
            'port' => 8983,
84
            'path' => '/solr',
85
            'version' => '4'
86
        );
87
88
        // Build some by-version defaults
89
        $version = isset(self::$solr_options['version']) ? self::$solr_options['version'] : $defaults['version'];
90
91
        /** @var Module $module */
92
        $module = ModuleLoader::getModule('silverstripe/fulltextsearch');
93
        $modulePath = $module->getPath();
94
95
        if (version_compare($version, '4', '>=')) {
96
            $versionDefaults = [
97
                'service'       => Solr4Service::class,
98
                'extraspath'    => $modulePath . '/conf/solr/4/extras/',
99
                'templatespath' => $modulePath . '/conf/solr/4/templates/',
100
            ];
101
        } else {
102
            $versionDefaults = [
103
                'service'       => Solr3Service::class,
104
                'extraspath'    => $modulePath . '/conf/solr/3/extras/',
105
                'templatespath' => $modulePath . '/conf/solr/3/templates/',
106
            ];
107
        }
108
109
        return (self::$merged_solr_options = array_merge($defaults, $versionDefaults, self::$solr_options));
110
    }
111
112
113
    public static function set_service_class($class)
114
    {
115
        user_error('set_service_class is deprecated - pass as part of $options to configure_server', E_USER_WARNING);
116
        self::configure_server(array('service' => $class));
117
    }
118
119
    /** @var SolrService | null - The instance of SolrService for core management */
0 ignored issues
show
The type SilverStripe\FullTextSearch\Solr\SolrService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
    protected static $service_singleton = null;
121
    /** @var [SolrService_Core] - The instances of SolrService_Core for each core */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [SolrService_Core] at position 0 could not be parsed: Unknown type name '[' at position 0 in [SolrService_Core].
Loading history...
122
    protected static $service_core_singletons = array();
123
124
    /**
125
     * Get a SolrService
126
     *
127
     * @param string $core Optional name of index class
128
     * @return SolrService_Core
0 ignored issues
show
The type SilverStripe\FullTextSearch\Solr\SolrService_Core was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
     */
130
    public static function service($core = null)
131
    {
132
        $options = self::solr_options();
133
134
        if (!self::$service_singleton) {
135
            self::$service_singleton = Injector::inst()->create(
136
                $options['service'],
137
                $options['host'],
138
                $options['port'],
139
                $options['path']
140
            );
141
        }
142
143
        if ($core) {
144
            if (!isset(self::$service_core_singletons[$core])) {
145
                self::$service_core_singletons[$core] = self::$service_singleton->serviceForCore(
146
                    singleton($core)->getIndexName()
147
                );
148
            }
149
150
            return self::$service_core_singletons[$core];
151
        } else {
152
            return self::$service_singleton;
153
        }
154
    }
155
156
    public static function get_indexes()
157
    {
158
        return FullTextSearch::get_indexes(SolrIndex::class);
159
    }
160
}
161