Giter Site home page Giter Site logo

outpost's Introduction

Outpost is a lightweight web framework, designed for decoupled websites. PHP 5.5+ is required.

What Outpost Does

Routing. Outpost uses Phroute to route an incoming request to the correct responder.

Caching. Each Outpost site has a Stash instance for storing resources between requests.

Logging. Outpost uses Monolog to send status messages to lots of types of logs.

What Outpost Doesn't Do

Templates. Outpost provides no default templating engine.

HTTP. Outpost doesn't provide a client for fetching external resources.

Quickstart

Create a new directory for your Outpost installation. From inside this directory, use Composer to install Outpost:

composer require pixo/outpost

You should now have composer.json and composer.lock files, and a vendor directory containing Outpost and its dependencies.

Create a new directory called public, and in that directory make a new file called index.php with the following contents:

<?php

# Get the Composer autoloader
require_once __DIR__ . '/../vendor/autoload.php';

# Get the incoming request
$request = Symfony\Component\HttpFoundation\Request::createFromGlobals();

# Create a Site object
$site = new Outpost\Site();

# Add an example route
$site->addRoute('GET', '/', function () { print "Hello."; });

# Send a response
$site->respond($request);

Start a local PHP development server and point it at the public directory:

php -S 0.0.0.0:8080 -t public

Once the server is running, you should be able to visit http://localhost:8080/ and see the following:

Hello.

What Just Happened?

Outpost received a request for the home page, and it routed the request to a function that printed "Hello."

When you visited http://localhost:8080/, the server directed you to the index.php script. It included the Composer autoloader, then made a new Request object, using information from the server environment.

The script next created an Outpost Site object, and added one routing instruction: when a visitor asks for the home page, run this function. Functions or other callables used as the targets of routes are called Responders.

Finally, the new Site's respond() method was called. The router used the Request object to find the right Responder: a function that printed "Hello."

Sites

Site objects have two primary purposes:

  • They route each incoming request to the appropriate Responder.
  • They provide Resources needed to create responses.

Responders

Responders act as router callbacks, and are expected to output a response when invoked.

Responder routes can be created using the site's addRoute() method:

$site->addRoute('GET', '/news', new NewsPageResponder());
$site->addRoute('GET', '/news/article/{id}', new ArticlePageResponder());

Responders receive 3 parameters when invoked:

  1. The Site object responding to the request
  2. The Request object
  3. Any parameters extracted from the URL
class ArticlePageResponder
{
  public function __invoke($site, $request, array $params)
  {
    $articleId = list($params);
    print $this->render('about-page.tpl', $this->getArticle($articleId));
  }
}

Resources

An Outpost installation may define any number of Resource classes. Resources are retrieved using the Site's get() method, and they receive the Site object when invoked. The simplest resource is just a callable:

$resource = function ($site) { return 1; }
print $site->get($resource);

The get() method invokes the callable and returns the result, so the output would be:

1

Caching

Resources that implement CacheableInterface can be stored in the site cache, and are only invoked when the cached resource is missing or stale. Cacheable resources have a unique key, and specify the number of seconds they may be cached before a refresh is required.

class ExampleExpensiveResource implements \Outpost\Cache\CacheableInterface {

  public function __invoke($site) {
    # Something that takes a long time, then...
    return $this;
  }
  
  public function getCacheKey() {
    return 'examples/expensive';
  }
  
  public function getCacheLifetime() {
    return 3600; # 1 hour
  }
}

The first time this resource is requested, it is invoked, and the return value is stored in the site cache. For subsequent requests, the cached copy is returned, until the copy is older than the value of getCacheLifetime().

# Nothing in the cache for this call, so Outpost invokes the Resource
# and caches the return value.
$fresh = $site->get(new ExampleExpensiveResource());

# This time the Resource is in the cache, so Outpost returns the cached Resource.
$cached = $site->get(new ExampleExpensiveResource());

# An hour passes...

# Now the cached copy is stale, so Outpost will invoke the Resource again,
# and replace the cached copy.
$fresh = $site->get(new ExampleExpensiveResource());

The Site::getCache() method provides access to the underlying Stash cache object.

# Clear a specific key
$site->getCache()->clear('cache/key');

# Clear a range of keys
$site->getCache()->clear('things/id/*');

# Clear the whole cache
$site->getCache()->clear();

# Flush the cache
$site->getCache()->flush();

outpost's People

Contributors

mattsharkey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.