forked from nickbart/php-plex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plex.php
192 lines (181 loc) · 6.38 KB
/
Plex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Plex Bootstrap
*
* This is the file to be included in your application and will bootstrap the
* rest of what is required.
*
* @category php-plex
* @package Plex
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.2
*
* This file is part of php-plex.
*
* php-plex 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 3 of the License, or
* (at your option) any later version.
*
* php-plex 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.
*/
$phpPlexDir = dirname(__FILE__);
// Exception
require_once(sprintf('%s/Exception/ExceptionInterface.php', $phpPlexDir));
require_once(sprintf('%s/Exception/ExceptionAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Machine.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Server.php', $phpPlexDir));
require_once(sprintf('%s/Exception/Server/Library.php', $phpPlexDir));
// Machine
require_once(sprintf('%s/Machine/MachineInterface.php', $phpPlexDir));
require_once(sprintf('%s/Machine/MachineAbstract.php', $phpPlexDir));
// Server
require_once(sprintf('%s/Server.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/SectionAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Section/Movie.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Section/Show.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Section/Artist.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Section/Photo.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/ItemInterface.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/ItemAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/ItemGrandparentAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/ItemParentAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/ItemChildAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Movie.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Show.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Season.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Episode.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Artist.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Album.php', $phpPlexDir));
require_once(sprintf('%s/Server/Library/Item/Track.php', $phpPlexDir));
// Client
require_once(sprintf('%s/Client.php', $phpPlexDir));
require_once(sprintf('%s/Client/ControllerAbstract.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Navigation.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Playback.php', $phpPlexDir));
require_once(sprintf('%s/Client/Controller/Application.php', $phpPlexDir));
/**
* Bootstrap class for using php-plex to interact with the Plex HTTP API.
*
* @category php-plex
* @package Plex
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.2
*/
class Plex
{
/**
* A list of Plex server machines on the network. This is defined by the
* instantiating software.
* @var Plex_Server[]
*/
private static $servers = array();
/**
* A list of the Plex client machines on the network This is found upon
* registring of Plex server. The first registered Plex server will go out
* and get the list of available clients and register them accordingly.
* @var Plex_Client[]
*/
private static $clients = array();
/**
* Allows an instantiating software to define a list of Plex servers on the
* network. In addition, the first server listed will be used to find the
* list of available clients and will register them accordingly.
*
* @param array $servers An associative array of Plex server machines on the
* network defined thusly:
*
* array (
* 'server-1-name' => array(
* 'address' => '192.168.1.5',
* 'port' => 32400
* ),
* 'server-2-name' => array(
* 'address' => '192.168.1.10',
* 'port' => 32400
* )
* )
*
* @uses Plex::$servers
* @uses Plex::registerClients()
* @uses Plex::getServer()
* @uses Plex_Server::getClient()
*
* @return void
*/
public function registerServers(array $servers)
{
// Register each server.
foreach ($servers as $name => $server) {
$port = isset($server['port']) ? $server['port'] : NULL;
self::$servers[$name] = new Plex_Server(
$name,
$server['address'],
$port
);
}
// We are going to use the first server in the list to get a list of the
// availalble clients and register those automatically.
$serverName = reset(array_keys(self::$servers));
$this->registerClients(
$this->getServer($serverName)->getClients()
);
}
/**
* Registers each found client with the bootstrap, so they can be found and
* used by the instantiating software.
*
* @param Plex_Client[] $clients An associative array of Plex client machines on the
* network.
*
* @uses Plex::$clients
*
* @return void
*/
private function registerClients(array $clients)
{
self::$clients = $clients;
}
/**
* Returns the requested server by the unique name under which it was registered.
*
* @param string $serverName The unique name of the requested server.
*
* @uses Plex::$servers
*
* @return Plex_Server The requested Plex server machine.
*
* @throws Plex_Exception_Server
*/
public function getServer($serverName)
{
if (!isset(self::$servers[$serverName])) {
throw new Plex_Exception_Server(
'RESOURCE_NOT_FOUND',
array($serverName)
);
}
return self::$servers[$serverName];
}
/**
* Returns the requested client by the unique name under which it was registered.
*
* @param string $clientName The unique name of the requested client.
*
* @uses Plex::$clients
*
* @return Plex_Client The requested Plex client machine.
*/
public function getClient($clientName)
{
return self::$clients[$clientName];
}
}