/** * @param $connection * @param array $config * @throws Exception\RuntimeException */ public function __construct($connection, $config = []) { // @codeCoverageIgnoreStart if (!extension_loaded('mongo') || !class_exists('MongoClient') || version_compare(MongoClient::VERSION, '1.4.1', '<')) { throw new Exception\RuntimeException('The Mongo Driver v1.4.1 required for this adapter to work'); } // @codeCoverageIgnoreEnd parent::__construct($connection, $config); }
/** * @param null $connection * @param array $config * @throws \yii\base\InvalidConfigException * @throws \yii\mongodb\Exception */ public function __construct($connection = null, $config = []) { if ($connection === null) { if ($this->connection !== null && \Yii::$app->has($this->connection)) { $db = \Yii::$app->get($this->connection); if (!$db instanceof \yii\mongodb\Connection) { throw new \yii\base\InvalidConfigException('Connection component must implement \\yii\\mongodb\\Connection.'); } $connection = $db->getDatabase()->mongoDb; } } else { $connection = new \yii\mongodb\Connection(['dsn' => $this->dsn]); $connection = $connection->getDatabase()->mongoDb; } parent::__construct($connection, $config); }
<?php require_once __DIR__ . '/../../vendor/autoload.php'; use Chadicus\Slim\OAuth2\Routes; use Chadicus\Slim\OAuth2\Middleware; use Slim\Slim; use OAuth2\Server; use OAuth2\Storage; use OAuth2\GrantType; $mongoDb = (new MongoClient())->selectDb('slim_oauth2'); $storage = new Storage\Mongo($mongoDb); $storage->setClientDetails('librarian', 'secret', '/receive-code', null, 'bookCreate'); $storage->setClientDetails('student', 's3cr3t'); $server = new Server($storage, ['access_lifetime' => 3600], [new GrantType\ClientCredentials($storage), new GrantType\AuthorizationCode($storage)]); $app = new Slim(); Routes\Token::register($app, $server); Routes\Authorize::register($app, $server); Routes\ReceiveCode::register($app); $app->config('templates.path', __DIR__ . '/../../vendor/chadicus/slim-oauth2-routes/templates'); $authorization = new Middleware\Authorization($server); $authorization->setApplication($app); $app->get('/books', $authorization, function () use($app, $mongoDb) { $result = []; try { $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5; $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0; $books = $mongoDb->books->find([])->skip($offset)->limit($limit); $result = ['offset' => $offset, 'limit' => $books->count(true), 'total' => $books->count(), 'books' => []]; foreach ($books as $book) { $result['books'][] = ['id' => (string) $book['_id'], 'url' => "/books/{$book['_id']}"]; }
/** * @param $connection * @param array $config * @throws Exception\RuntimeException */ public function __construct($connection, $config = []) { // @codeCoverageIgnoreStart if (!(extension_loaded('mongodb') || extension_loaded('mongo')) || !class_exists(MongoClient::class) || version_compare(MongoClient::VERSION, '1.4.1', '<')) { throw new Exception\RuntimeException('The MongoAdapter requires either the Mongo Driver v1.4.1 or ' . 'ext/mongodb + the alcaeus/mongo-php-adapter package (which provides ' . 'backwards compatibility for ext/mongo classes)'); } // @codeCoverageIgnoreEnd parent::__construct($connection, $config); }