oauth2-server/src/AbstractServer.php

103 lines
3.0 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;
2015-04-05 17:03:13 +01:00
use League\Container\Container;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use League\Event\EmitterAwareInterface;
use League\Event\EmitterTrait;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
2015-04-05 21:13:45 +01:00
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
2014-01-10 17:30:12 +00:00
use Symfony\Component\HttpFoundation\Request;
/**
* OAuth 2.0 Resource Server
*/
2015-04-05 17:03:13 +01:00
abstract class AbstractServer implements ContainerAwareInterface, EmitterAwareInterface
2014-01-10 17:30:12 +00:00
{
2015-04-05 17:03:13 +01:00
use EmitterTrait, ContainerAwareTrait;
2014-01-10 17:30:12 +00:00
/**
* The request object
*
2014-11-07 00:46:02 +00:00
* @var \Symfony\Component\HttpFoundation\Request
2014-01-10 17:30:12 +00:00
*/
protected $request;
/**
2015-04-05 17:03:13 +01:00
* Setup the server
*/
public function __construct()
{
2015-04-05 17:03:13 +01:00
$this->setContainer(new Container());
$this->getContainer()->singleton('emitter', $this->getEmitter());
$this->getContainer()->addServiceProvider('League\OAuth2\Server\ServiceProviders\ClientCredentialsGrantServerProvider');
}
2014-01-10 17:30:12 +00:00
/**
* Sets the Request Object
2014-12-10 13:10:35 +00:00
*
2014-01-10 17:30:12 +00:00
* @param \Symfony\Component\HttpFoundation\Request The Request Object
2014-12-10 13:10:35 +00:00
*
2014-01-10 17:30:12 +00:00
* @return self
2015-04-05 17:03:13 +01:00
* @deprecated
2014-01-10 17:30:12 +00:00
*/
2014-09-22 12:56:15 +07:00
public function setRequest($request)
2014-01-10 17:30:12 +00:00
{
$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.
2014-12-10 13:10:35 +00:00
*
2014-01-10 17:30:12 +00:00
* @return \Symfony\Component\HttpFoundation\Request
2015-04-05 17:03:13 +01:00
* @deprecated
2014-01-10 17:30:12 +00:00
*/
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;
}
/**
2015-04-05 17:03:13 +01:00
* Add a repository to the server
2014-12-10 13:10:35 +00:00
*
2015-04-05 17:03:13 +01:00
* @param RepositoryInterface $repository
*/
2015-04-05 17:03:13 +01:00
public function addRepository(RepositoryInterface $repository)
{
2015-04-05 17:03:13 +01:00
switch ($repository) {
case ($repository instanceof AccessTokenRepositoryInterface):
$this->getContainer()->add('AccessTokenRepository', $repository);
break;
case ($repository instanceof ClientRepositoryInterface):
$this->getContainer()->add('ClientRepository', $repository);
break;
case ($repository instanceof ScopeRepositoryInterface):
$this->getContainer()->add('ScopeRepository', $repository);
break;
2015-04-05 21:13:45 +01:00
case ($repository instanceof UserRepositoryInterface):
$this->getContainer()->add('UserRepository', $repository);
break;
2015-04-05 17:03:13 +01:00
}
2014-01-10 17:30:12 +00:00
}
2014-05-03 10:53:43 +01:00
}