brtriver / BEAR.Sunday

PHP 5.4+ resource oriented web framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BEAR, a resource oriented framework for PHP5.4

  • 0.3.0 Build Status

One minute example

RESTful Service

Resource object

<?php
    class Greetings extends AbstractObject
    {
        public function onGet($lang = 'en')
        {
            $this['greeting'] = $this->message[$lang];
            return $this;
        }
    }

Resource client

<?php
    use ResourceInject;
    
    $this->resource->get->uri('app://self/greetings')->withQuery(['lang' => 'ja'])->eager->request();

Dependency injection

Bidning

<?php
    $this->bind('Doctrine\Common\Annotations')
         ->to('Doctrine\Common\Annotations\FileCacheReader')
         ->in(Scope::SINGLETON);

Consumer

<?php
    /**
     * @Inject
     */
    public function __construct(Annotations $reader)
    {
        $this->reader = $reader;
    }

Aspect oriented programing

Interceptor

<?php
    class WeekendBlocker implements MethodInterceptor
    {
        public function invoke(MethodInvocation $invocation)
        {
            $today = getdate();
            if ($today['weekday'][0] === 'S') {
                throw new \RuntimeException(
                      $invocation->getMethod()->getName() . " not allowed on weekends!"
                );
            }
            return $invocation->proceed();
        }
    }

Service

<?php
    class RealBillingService implements BillingService
    {
        /**
         * @WeekendBlock
         */
        public function chargeOrder()
        {
            ...
        }
    }

Weaving an Interceptor

<?php
    $this->bindInterceptor(
        $this->matcher->any(),
        $this->matcher->annotatedWith('WeekendBlock'),
        [new WeekendBlocker]
    );

Dependencies

Rahter than reinvent the wheel and develop our library, BEAR.Sunday use (or will use) these great libraries.

Requirement

optional

php.ini

apc.enable_cli = 1
xhprof.output_dir = /tmp

Testing BEAR.Sunday

Here's how to install BEAR.Sunday:

$ git clone git://github.com/koriym/BEAR.Sunday.git
$ cd BEAR.Sunday
$ wget http://getcomposer.org/composer.phar
$ php ./composer.phar install
$ chmod -R 777 apps/sandbox/tmp apps/sandbox/log

buil-in web server

$ cd apps/sandbox/htdocs
$ php -S localhost:8088 web.php
$ curl http://localhost:8088/

$ php -S localhost:8089 api.php
$ curl http://localhost:8089/blog/posts

CLI

$ php web.php get /index
$ php api.php get app://self/greetings?lang=ja
$ php api.php get app://self/greetings?lang=en
$ php api.php get page://self/index

About

PHP 5.4+ resource oriented web framework.

License:BSD 3-Clause "New" or "Revised" License