oauth2-server/src/AbstractServer.php

140 lines
3.1 KiB
PHP
Raw Normal View History

2014-01-10 17:30:12 +00:00
<?php
/**
* OAuth 2.0 Abstract Server
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
2014-01-10 17:30:12 +00:00
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
2014-01-10 17:30:12 +00:00
*/
namespace League\OAuth2\Server;
2014-04-23 17:02:50 +01:00
use League\OAuth2\Server\Exception;
use League\OAuth2\Server\TokenType\TokenTypeInterface;
2014-01-10 17:30:12 +00:00
use Symfony\Component\HttpFoundation\Request;
use League\Event\Emitter;
2014-01-10 17:30:12 +00:00
/**
* OAuth 2.0 Resource Server
*/
abstract class AbstractServer
{
/**
* The request object
*
* @var Util\RequestInterface
*/
protected $request;
/**
* Storage classes
* @var array
*/
protected $storages = [];
2014-04-23 17:02:50 +01:00
/**
* Token type
* @var TokenTypeInterface
*/
protected $tokenType;
/**
* Event emitter
*/
protected $eventEmitter;
/**
* Abstract server constructor
*/
public function __construct()
{
$this->setEventEmitter();
}
/**
* Set an event emitter
* @param object $emitter Event emitter object
*/
public function setEventEmitter($emitter = null)
{
if ($emitter === null) {
$this->eventEmitter = new Emitter;
} else {
$this->eventEmitter = $emitter;
}
}
public function addEventListener($eventName, callable $listener)
{
$this->eventEmitter->addListener($eventName, $listener);
}
public function getEventEmitter()
{
return $this->eventEmitter;
}
2014-01-10 17:30:12 +00:00
/**
* Sets the Request Object
* @param \Symfony\Component\HttpFoundation\Request The Request Object
* @return self
*/
public function setRequest(Request $request)
{
$this->request = $request;
2014-05-03 10:53:43 +01:00
2014-01-10 17:30:12 +00:00
return $this;
}
/**
* Gets the Request object. It will create one from the globals if one is not set.
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
if ($this->request === null) {
2014-05-02 15:14:25 +01:00
$this->request = Request::createFromGlobals();
2014-01-10 17:30:12 +00:00
}
return $this->request;
}
/**
* Return a storage class
2014-05-03 10:53:43 +01:00
* @param string $obj The class required
2014-01-10 17:30:12 +00:00
* @return Storage\ClientInterface|Storage\ScopeInterface|Storage\SessionInterface
*/
public function getStorage($obj)
{
if (!isset($this->storages[$obj])) {
2014-04-25 11:24:42 +01:00
throw new Exception\ServerErrorException(
2014-01-16 16:50:16 +00:00
'The `'.$obj.'` storage interface has not been registered with the server'
2014-01-10 17:30:12 +00:00
);
}
2014-05-03 10:53:43 +01:00
2014-01-10 17:30:12 +00:00
return $this->storages[$obj];
}
2014-04-23 17:02:50 +01:00
/**
* Set the access token type
* @param TokenTypeInterface $tokenType The token type
* @return void
*/
public function setIdType(TokenTypeInterface $tokenType)
2014-04-23 17:02:50 +01:00
{
$this->tokenType = $tokenType;
}
/**
* Get the access token type
* @return TokenTypeInterface
*/
2014-07-11 18:27:03 +01:00
public function getTokenType()
2014-04-23 17:02:50 +01:00
{
return $this->tokenType;
}
2014-05-03 10:53:43 +01:00
}