Skip to content

Instantly share code, notes, and snippets.

@devosc
Last active August 29, 2015 14:08
Show Gist options
  • Save devosc/5b66b7080a6736d8d9d5 to your computer and use it in GitHub Desktop.
Save devosc/5b66b7080a6736d8d9d5 to your computer and use it in GitHub Desktop.
<?php
namespace Micro;
use Framework\Route\Definition\Builder\Builder;
use Framework\Route\Definition\Definition;
use Framework\Service\Config\Router\Router;
class Micro
implements MicroApplication
{
/**
*
*/
use Base;
/**
* @link https://github.com/mvc5/framework/blob/master/src/Route/Definition/Builder/Builder.php#L28
* @param array $definition
* @return Definition
*/
public function route(array $definition)
{
return Builder::add(
$this->routes[Args::DEFINITIONS],
$definition,
explode(
'/',
$definition[Definition::NAME]
),
function(Definition $parent, Definition $definition, $path, $root) {
$parent->add($path, $definition);
$root && $this->routes[Args::EVENTS]->add(Args::ROUTE_DISPATCH, new Router($definition));
return $definition;
},
true
);
}
/**
* @param array $args
* @param callable $callback
* @return callable|mixed|null|object
*/
public function __invoke(array $args = [], callable $callback = null)
{
return $this->call(Args::MICRO, $args, $callback);
}
}
<?php
/**
*
*/
use Micro\Micro;
use Framework\View\Model\Model;
use Framework\View\Manager\ViewManager;
include __DIR__ . '/../vendor/autoload.php';
$app = new Micro();
//services via ArrayAccess
//var_dump($app['Request']);
//configuration via property access
//$app->templates['layout'] = '../view/layout.phtml';
//$app->templates['home'] = '../view/index.phtml';
$app->route([
'name' => 'home',
'route' => '/',
'controller' => function(ViewManager $vm, array $args = []) {
$args['micro_route'] = 'micro:home';
//$vm is a named argument plugin
$args['demo_time'] = $vm->call('time');
return new Model('home', ['args' => $args]);
}
]);
$app->route([
'name' => 'application',
'route' => '/application',
'controller' => function(array $args = []) {
$args['micro_route'] = 'micro:application';
return new Model('home', ['args' => $args]);
}
]);
$app->route([
'name' => 'application/default',
//'route' => '[/:controller[/:action]]',
'controller' => function(array $args = []) {
/** @var $this Framework\Controller\Manager\Manager */
$args['micro_route'] = 'micro:application:default';
$args['demo_time'] = $this->call('time');
return new Model('home', ['args' => $args]);
}
]);
$app->route([
'name' => 'application/default/three',
'route' => '[/:controller[/:action]]',
'controller' => function(array $args = []) {
/** @var $this Framework\Controller\Manager\Manager */
$args['micro_route'] = 'micro:application:default:three';
$args['demo_time'] = $this->call('time');
return new Model('home', ['args' => $args]);
}
]);
call_user_func($app);
@jm42
Copy link

jm42 commented Nov 13, 2014

Could you made this something that work?

  • The config/config.php is missing.
  • The files have problems to be autoloaded because of \\ (not really a problem).
  • Which version of PHP are you using? I got a PHP Fatal error: Unsupported operand types in Application\Micro.php on line 22 with PHP 5.6.2.

I'm trying to test how much "micro" this is.

I'm using this composer.json:

{
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "devosc/micro",
        "version": "dev-master",
        "source": {
          "url": "https://gist.github.com/5b66b7080a6736d8d9d5.git",
          "type": "git",
          "reference": "master"
        },
        "autoload": {
          "files": [
            "Application_MicroApplication.php",
            "Application_Micro.php"
          ]
        }
      }
    }
  ],
  "require": {
    "mvc5/framework": "dev-master",
    "devosc/micro": "dev-master"
  },
  "scripts": {
    "post-install-cmd": "php fix.php"
  }
}

@devosc
Copy link
Author

devosc commented Nov 15, 2014

@jm42, I've updated the demo application and there is a new public/micro.php which uses the Micro application class. It can probably be anything you want, e.g a form of decorator, I quickly choose it to be a type of factory class since it uses the base factory trait convenience methods. There is a micro config directory which probably could be better, but it does demonstrate how different environments can have their own configuration (e.g development).

These need further consideration

initial configuration
Because everything is expected to be injected, I don't know how much of the initial configuration needs to become 'hidden' or maybe some sane defaults.

route url generator
I ran into problems with the url generator since the routes defined in public/micro.php are no longer nested.

base config ArrayAccess
It probably should just happen.

I'll need to think some more on the composer settings. It shouldn't really be a problem; I'm not sure if you want all the commonly required files included in the composer config but being explicit looks like a nice way to go.

"autoload": {
    "psr-0": {
        "": "./src"
    }
}

The above is what I think is causing the autoloader problems, trying adding it to composer. This can be changed as needed.

I'm using php 5.5.9 and 5.5.14

Either I missed the github notification, or it seems in Gist it maybe necessary to use the username call prefix @.

If needed, it should be possible to specify your own router so that may resolve the issue of building the url strings in the view.

@devosc
Copy link
Author

devosc commented Nov 15, 2014

@jm42, the url generation has been somewhat fixed. Before the definitions object was just a configuration object (it could of also been an array), however now it is a route definition object with containing child definitions.

@devosc
Copy link
Author

devosc commented Nov 16, 2014

@jm42 add "mvc5/micro": "@dev" to your composer file. The new repo is here.

@jm42
Copy link

jm42 commented Nov 18, 2014

I see. I dig into when I have time. Thanks for the response.

@devosc
Copy link
Author

devosc commented Nov 18, 2014

I may end up folding the array and property access into the main repo classes (Application and Web). I don't see much point in hiding the configuration and I'm thinking about a putting in a /framework/config directory at the same level as /src so the application can choose which defaults to use and for them to be easier to merge. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment