2017 © Pedro PelĂĄez
 

library media-exposer

A media path/source resolution library in PHP

image

knplabs/media-exposer

A media path/source resolution library in PHP

  • Tuesday, April 24, 2012
  • by Knplabs
  • Repository
  • 27 Watchers
  • 14 Stars
  • 4,565 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 1 Versions
  • 53 % Grown

The README.md

Media Exposer

The Media Exposer is a PHP (5.3+) library that helps you expose your media files to the clients of your web application., (*1)

Principle

When you develop a web application, you always need to set up a strategy to serve your medias to the clients. This is the problem this library tries to help you solve., (*2)

The Exposer is the main entry point of the library. It is responsible for returning the right source or the right path for the given media and options. To do that, it iterates over the registered resolvers to find the first one supporting the given media and options. The source or path generation is delegated to it. So the first thing to do when you want to set up an exposer is to register resolvers. When you register a resolver, you give it a priority., (*3)

All resolvers implement the Resolver interface. They indicate if they support a given media and options. But this only interface is not sufficient so a proper resolver must also implement either the SourceResolver interface or the _PathResolver_ (or both)., (*4)

Quick Exemple

Enough discussion, let's try it., (*5)

As an exemple, will take the following simple image model:, (*6)

<?php

class FooImage
{
    private $filename;

    public function getFilename()
    {
        return $this->filename;
    }
}

Then, we create an associated resolver:, (*7)

<?php

use MediaExposer\PathResolver,
    MediaExposer\SourceResolver;

class FooImageResolver implements PathResolver, SourceResolver
{
    /**
     * {@inheritDoc}
     */
    public function supports($media, array $options)
    {
        return $media instanceof FooImage;
    }

    /**
     * {@inheritDoc}
     */
    public function getPath($media, array $options)
    {
        return '/path/to/the/images/directory/' . $media->getFilename();
    }

    /**
     * {@inheritDoc}
     */
    public function getSource($media, array $options)
    {
        return '/media/foo/' . $media->getFilename();
    }

    /**
     * {@inheritDoc}
     */
    public function getSourceType($media, array $options)
    {
        return SourceResolver::TYPE_RELATIVE; // or SourceResolver::TYPE_ABSOLUTE
    }
}

Finally, you can register it in your exposer:, (*8)

<?php

use MediaExposer\Exposer;

$exposer = new Exposer('http://the-host');
$exposer->addResolver(new FooImageResolver(), 10);

The first Exposer's argument is called $baseUrl. It's only required if you want to generate absolute URLs with relative SourceResolver instances. If so, it will be prepended to the relative source returned by the resolver to turn it absolute., (*9)

The Versions

24/04 2012

dev-master

9999999-dev http://knplabs.com

A media path/source resolution library in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

file image media path source