Skip to content

Instantly share code, notes, and snippets.

@simensen
Created February 15, 2013 00:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simensen/7d04656573f2d3c45d6f to your computer and use it in GitHub Desktop.
Depot sample applications
<?php
// Customizations to $app at this point apply to any
// aspect of the application (console, webapp, etc.)
$app['depot.debug.entity'] = 'https://beau.io';
$app['depot.core.model.auth.authFactory'] = $app->share(function() {
return new Depot\Core\Model\Auth\AuthFactory;
});
$app['depot.core.service.serializer.serializerFactory'] = $app->share(function() use ($app) {
return new Depot\Core\Service\Serializer\SerializerFactory($app['depot.core.model.auth.authFactory']);
});
$app['depot.core.service.serializer.serializer'] = $app->share(function() use ($app) {
return $app['depot.core.service.serializer.serializerFactory']->create();
});
$app['depot.core.model.entity.entityRepository'] = $app->share(function() use ($app) {
// Use an in-memory implementation of the Entity Repository.
$entityRepository = new Depot\Core\Model\Entity\MemoryEntityRepository;
// Define the JSON representation of our fictional entities profile.
$json = array(
'https://tent.io/types/info/core/v0.1.0' => array(
// Use a hard coded entity for debugging purposes.
'entity' => $app['depot.debug.entity'],
'licenses' => array(),
'servers' => array(
// Generate an absolute URL to our API root.
$app['url_generator']->generate('depot.routes.root', array(), true)
),
'version' => 21,
'permissions' => array(
'public' => true,
),
'tent_version' => '0.2',
),
'https://tent.io/types/info/basic/v0.1.0' => array(
'name' => 'Beau Simensen',
'avatar_url' => 'http://www.gravatar.com/avatar/14d0ad662a18587c0ebf786a656a697c',
'bio' => 'I write code. I like visual arts and music.',
'gender' => 'mail',
'location' => 'Anywhere, USA',
'birthdate' => '',
'website_url' => 'http://beausimensen.com',
'permissions' => array(
'public' => true,
),
'version' => 11,
),
);
// Denomralize our JSON entity to an object.
$entity = $app['depot.core.service.serializer.serializer']->denormalize(
$json,
'Depot\Core\Model\Entity\EntityInterface'
);
// Store it! Next time we request https://beau.io from the repository
// it should return the entity we just created.
$entityRepository->store($entity);
return $entityRepository;
});
<?php
// Customizations to $app at this point only apply to the
// web application application.
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app['dispatcher'] = $app->share($app->extend('dispatcher', function($dispatcher) use ($app) {
return new Depot\Api\Server\Symfony\EventDispatcher\PimpleContainerAwareEventDispatcher($dispatcher, $app);
}));
$app['depot.api.server.symfony.view.jsonResponseFactory'] = $app->share(function() use ($app) {
return new Depot\Api\Server\Symfony\View\JsonResponseFactory(
$app['depot.core.service.serializer.serializer']
);
});
$app['depot.api.server.symfony.view.viewFactory'] = $app->share(function() use ($app) {
return new Depot\Api\Server\Symfony\View\ViewFactory(
$app['depot.api.server.symfony.view.jsonResponseFactory']
);
});
$app['depot.api.server.symfony.view.view'] = $app->share(function() use ($app) {
return $app['depot.api.server.symfony.view.viewFactory']->create();
});
$app['depot.api.server.symfony.eventListener.viewListener.class'] = 'Depot\Api\Server\Symfony\EventListener\ViewListener';
$app['depot.api.server.symfony.eventListener.viewListener'] = $app->share(function() use ($app) {
return new Depot\Api\Server\Symfony\EventListener\ViewListener(
$app['depot.api.server.symfony.view.view']
);
});
$app['depot.api.server.symfony.eventListener.entityListener.class'] = 'Depot\Api\Server\Symfony\EventListener\EntityListener';
$app['depot.api.server.symfony.eventListener.entityListener'] = $app->share(function() use ($app) {
return new Depot\Api\Server\Symfony\EventListener\EntityListener(
$app['depot.core.model.entity.entityRepository']
);
});
$app['dispatcher'] = $app->share($app->extend('dispatcher', function($dispatcher) use ($app) {
$dispatcher->addSubscriberService(
'depot.api.server.symfony.eventListener.viewListener',
$app['depot.api.server.symfony.eventListener.viewListener.class']
);
$dispatcher->addSubscriberService(
'depot.api.server.symfony.eventListener.entityListener',
$app['depot.api.server.symfony.eventListener.entityListener.class']
);
return $dispatcher;
}));
$app->before(function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
// Use a hard coded entity for this request.
$request->attributes->set('depot.entity', $app['depot.debug.entity']);
// We will be able to use this to determine the effective entity based
// on request for multi-tenant installations. For example, we could check
// based on hostname patterns (https://*.foo.io or https://foo.io/*)
// for a user => entity mapping and use it here.
//
// Example: "simensen.foo.io" could be matched against "*.foo.io" to
// determine the incoming request is for to username "simensen". We
// could check username "simensen" to see what their registered entity
// is and set the effective entity to the correct URI. This way the
// rest of the system (mainly the controllers) do not have to be aware
// of "users" or even if they are a part of a multi-tenant installation
// or a single-tenant installation.
});
<?php
// Register additional controllers here.
//
// Define the controllers as services
//
$app['depot.api.server.controller.profile.getProfileController'] = $app->share(function() use ($app) {
return new Depot\Api\Server\Symfony\Controller\Profile\GetProfileController(
$app['depot.core.model.entity.entityRepository']
);
});
//
// Map the routing for our controller services
//
$app->get('/profile', 'depot.api.server.controller.profile.getProfileController:action')
->bind('depot.routes.profile.getProfile');
//
// This is its own thing specific to this application and probably
// won't get a service created for it. :)
//
$app->get('/', function() use ($app) {
$response = new Symfony\Component\HttpFoundation\Response;
$response->setContent('Tent!');
// Generate an aboslute URL to the profile endpoint of our server application.
// While not the root of the API, the profile endpoint serves as the entry
// point to the Tent.io API.
$profileUri = $app['url_generator']->generate(
'depot.routes.profile.getProfile',
array(),
true
);
// This Link header will make the root of our application a Tent.io discovery URL.
// This will instruct Tent-aware applications visit the profile endpoint for this server.
$response->headers->set('Link', sprintf('<%s>; rel="https://tent.io/rels/profile"', $profileUri));
return $response;
})->bind('depot.routes.root');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment