/** * Registers a custom temporary file * @param temporary file $tmp */ public function registerTmpFile($tmp) { $configuration = new Configuration(); $configuration->setAttribute('configFilePath', sys_get_temp_dir()); $configuration->setAttribute('configFileName', basename($tmp)); JsonDriver::register($configuration); }
public function _getDefaultDrestConfig() { $config = new Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->addPathsToConfigFiles(array(__DIR__ . '/Entities')); $config->setDebugMode(true); return $config; }
/** * Create a metadata manager instance * @param Configuration $config */ public function __construct(Configuration $config) { $driver = $config->getMetadataDriverClass(); $this->metaDataDriver = $driver::create($config->getPathsToConfigFiles()); $this->metadataFactory = new MetadataFactory($this->metaDataDriver); if ($cache = $config->getMetadataCacheImpl()) { $this->metadataFactory->setCache($cache); } }
/** * Get the error handler object, if none has been injected use default from config * @return AbstractHandler $error_handler */ public function getErrorHandler() { if (!$this->error_handler instanceof AbstractHandler) { // Force creation of an instance of the default error handler $className = $this->config->getDefaultErrorHandlerClass(); $this->error_handler = new $className(); } return $this->error_handler; }
/** * Attempt to match a representation * * @param AbstractRepresentation|string $representation * @param array $representationObjects * @return AbstractRepresentation|null * @throws RepresentationException */ protected function matchRepresentation($representation, array &$representationObjects) { if (!is_object($representation)) { $className = $this->getRepresentationClassName($representation); $representationObjects[] = $representation = new $className(); } if (!$representation instanceof AbstractRepresentation) { throw RepresentationException::representationMustBeInstanceOfDrestRepresentation(); } if (($representation = $this->determineRepresentationByHttpMethod($representation, $this->config->getDetectContentOptions())) !== null) { return $representation; } return null; }
/** * @expectedException \Drest\Route\NoMatchException */ public function testAllowOptionsSetToFalse() { $config = new Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->addPathsToConfigFiles(array(__DIR__ . '/../../Entities')); $config->setDebugMode(true); $config->setAllowOptionsRequest(false); $dm = $this->_getDrestManager(null, $config); $request = \Symfony\Component\HttpFoundation\Request::create('/user/1', 'OPTIONS'); $dm->dispatch($request); }
public function testFallbackToDefaultRepresentations() { $config = new Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->addPathsToConfigFiles(array(__DIR__ . '/../../Entities/Typical')); $config->setDefaultRepresentations(array('Json')); $config->setDebugMode(true); $dm = $this->_getDrestManager(null, $config); $request = \Symfony\Component\HttpFoundation\Request::create('/no_rep/1', 'GET'); $response = $dm->dispatch($request); // This should 404 (as it doesnt exist) $this->assertJsonStringEqualsJsonString('{"error":["An unknown error occurred"]}', $response->getBody()); }
public function testNoRouteMatchExceptionReturnedInAcceptedFormat() { $config = new Configuration(); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->addPathsToConfigFiles(array(__DIR__ . '/../Entities/Typical')); $config->setDefaultRepresentations(array('Json')); $config->setDebugMode(false); // Triggers an actual return document $dm = $this->_getDrestManager(null, $config); $representation = new \DrestCommon\Representation\Json(); $request = \Symfony\Component\HttpFoundation\Request::create('/this-doesnt-exist', 'GET', [], [], [], array('HTTP_ACCEPT' => $representation->getContentType())); /** @var \DrestCommon\Response\Response $response */ $response = $dm->dispatch($request); $this->assertInstanceOf('DrestCommon\\Response\\Response', $response); // Ensure we can decode as a json string $responseDocument = json_decode($response->getBody(), true); $this->assertTrue(isset($responseDocument['error'])); }
/** * Set up the HTTP manager * @param $request * @param $response * @param Configuration $config */ public function setUpHttp($request, $response, Configuration $config) { $this->setRequest(Request::create($request, $config->getRegisteredRequestAdapterClasses())); $this->setResponse(Response::create($response, $config->getRegisteredResponseAdapterClasses())); }
// Add the entities namespace to the loader $loader->add('Entities', __DIR__ . '/../'); $loader->add('Action', __DIR__ . '/../'); $loader->add('MyEvents', __DIR__ . '/../'); // Create an example doctrine application $ormConfig = new \Doctrine\ORM\Configuration(); $pathToEntities = array(__DIR__ . '/../Entities'); $ORMDriver = $ormConfig->newDefaultAnnotationDriver($pathToEntities, false); $ormConfig->setMetadataDriverImpl($ORMDriver); // Do proxy stuff $ormConfig->setProxyDir(__DIR__ . '/Entities/Proxies'); $ormConfig->setProxyNamespace('Entities\\Proxies'); $ormConfig->setAutoGenerateProxyClasses(true); $em = \Doctrine\ORM\EntityManager::create(array('host' => 'localhost', 'user' => 'developer', 'password' => 'developer', 'dbname' => 'drest', 'driver' => 'pdo_mysql'), $ormConfig); /********************START SETTING UP DREST CONFIGURATION***************************/ $drestConfig = new Configuration(); $drestConfig->setDetectContentOptions(array(Configuration::DETECT_CONTENT_HEADER => 'Accept', Configuration::DETECT_CONTENT_EXTENSION => true, Configuration::DETECT_CONTENT_PARAM => 'format')); $drestConfig->setExposureDepth(3); $drestConfig->setExposeRequestOption(Configuration::EXPOSE_REQUEST_PARAM_GET, 'expose'); $drestConfig->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); //$drestConfig->setDebugMode(true); $drestConfig->addPathsToConfigFiles($pathToEntities); // Set up event manager $evm = new Event\Manager(); //$evm->addEventListener(array('preServiceAction', 'postServiceAction', 'preRouting', 'postRouting', 'preDispatch', 'postDispatch'), new MyEvents\MyEvent()); // Set up the service action registry $serviceActions = new Drest\Service\Action\Registry(); $serviceActions->register(new Action\Custom(), ['Entities\\User::get_user2', 'Entities\\User::get_user3']); $emr = \Drest\EntityManagerRegistry::getSimpleManagerRegistry($em); $drestManager = \Drest\Manager::create($emr, $drestConfig, $evm, $serviceActions); echo $drestManager->dispatch();
/** * @expectedException \Drest\DrestException */ public function testAddingInvalidBasePath() { $config = new Configuration(); $path = new \StdClass(); $config->addRouteBasePath($path); }
// Add the entities namespace to the loader $loader->add('Entities', __DIR__ . '/../'); $loader->add('Action', __DIR__ . '/../'); $loader->add('MyEvents', __DIR__ . '/../'); // Create an example doctrine application $ormConfig = new \Doctrine\ORM\Configuration(); $pathToEntities = array(__DIR__ . '/../Entities'); $ORMDriver = $ormConfig->newDefaultAnnotationDriver($pathToEntities, false); $ormConfig->setMetadataDriverImpl($ORMDriver); // Do proxy stuff $ormConfig->setProxyDir(__DIR__ . '/Entities/Proxies'); $ormConfig->setProxyNamespace('Entities\\Proxies'); $ormConfig->setAutoGenerateProxyClasses(true); $em = \Doctrine\ORM\EntityManager::create(array('host' => 'localhost', 'user' => 'developer', 'password' => 'developer', 'dbname' => 'drest', 'driver' => 'pdo_mysql'), $ormConfig); /********************START SETTING UP DREST CONFIGURATION***************************/ $drestConfig = new Configuration(); $drestConfig->setDetectContentOptions(array(Configuration::DETECT_CONTENT_HEADER => 'Accept', Configuration::DETECT_CONTENT_EXTENSION => true, Configuration::DETECT_CONTENT_PARAM => 'format')); $drestConfig->setExposureDepth(3); $drestConfig->setExposeRequestOption(Configuration::EXPOSE_REQUEST_PARAM_GET, 'expose'); $drestConfig->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); //$drestConfig->setDebugMode(true); // Use the PHP Driver $drestConfig->setMetadataDriverClass('\\Drest\\Mapping\\Driver\\PhpDriver'); $drestConfig->addPathsToConfigFiles([__DIR__ . '/../config/php/user.php', __DIR__ . '/../config/php/profile.php']); // Set up event manager $evm = new Event\Manager(); //$evm->addEventListener(array('preServiceAction', 'postServiceAction', 'preRouting', 'postRouting', 'preDispatch', 'postDispatch'), new MyEvents\MyEvent()); $emr = \Drest\EntityManagerRegistry::getSimpleManagerRegistry($em); $drestManager = \Drest\Manager::create($emr, $drestConfig, $evm); echo $drestManager->dispatch(); //echo $drestManager->dispatch(null, null, 'Entities\User::get_user', array('id' => 1));
/** * */ public static function register(Configuration $config) { self::$configuration_filepath = $config->getAttribute('configFilePath'); self::$configuration_filename = $config->getAttribute('configFileName'); }