无框架/无框架

快速、灵活、轻量级的PHP 5.5框架

dev-master 2015-05-05 05:07 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:10:03 UTC


README

(PHP 5 >= 5.5)

  • PSR-0 文件自动加载
  • 依赖注入
  • 延迟对象创建
  • 魔法式缓存
  • 分层配置
  • 如果没有定义类,尝试通过命名空间和属性名猜测它(因为有了PSR-0)
  • 虚拟数据库
  • ORM
  • 只有24个不很臃肿的类

如何使用

<?php
require __DIR__ . '/../class.php/NoFramework/Config.php';

// Read comments in Config.php for yaml examples
// example.yaml should reside in '.config' up from script path
// (i.e. '../.config' or '../../.config', and so on until found)

(new NoFramework\Config)->parse('example.yaml')->application->start();
<?php
require __DIR__ . '/../class.php/NoFramework/Autoload.php';

(new NoFramework\Autoload)->register();

(new NoFramework\Autoload([
    'namespace' => 'Example',
    'path' => __DIR__ . '/../class.php/Example',
])->register();

(new Example\Factory([

    // Object of class Example\Logger will be instantiated as property 'log'
    // in object of class Example\Service instantiated in property 'service'.
    // If Example\Service does not exist, then it will be Example\Factory

    'service.log' => ['$new' => 'Logger'],

]))->service->start();
class SomeClass
{
    use \NoFramework\Magic;

    protected $default = 'default value';

    // Memoization
    // (same as default value, but calculated)
    protected function __property_pid()
    {
        echo 'calculated' . PHP_EOL;

        return posix_getpid();
    }

    public function callMe()
    {
        var_dump($this->pid);
        var_dump($this->pid);
        var_dump($this->pid);
    }
}

最小HTTP应用

假设我们在/home/example.com创建项目

你可能希望创建用户,而不仅仅是目录

useradd example.com

将NoFramework和Twig复制到/home/example.com/class.php目录

git clone https://github.com/NoFramework/NoFramework
git clone https://github.com/fabpot/Twig

创建/home/example.com/.cache并使其对php-fpm用户或所有人可写

创建/home/example.com/index.php

<?php
namespace NoFramework;

$debug = true;

ini_set('date.timezone', 'UTC');

ini_set('display_startup_errors', $debug);
ini_set('display_errors', $debug);

require __DIR__ . '/class.php/NoFramework/Autoload.php';

(new Autoload)->register();

(new Autoload([
    'namespace' => 'Twig',
    'path' => __DIR__ . '/class.php/Twig/lib/Twig',
    'separator' => '_',
]))->register();

(new Http\Application([
    'namespace' => __NAMESPACE__,
    'template' => ['$new' => [
        'class' => 'Template\Twig',
        'path' => __DIR__ . '/template',
        'cache' => __DIR__ . '/.cache/twig',
        'search_path' => 'landing',
        'auto_reload' => $debug,
        'debug' => $debug, // enable 'dump' function
    ]]
]))->start();

编辑服务器上的/etc/hosts并显式设置example.com的IP

创建/home/example.com/nginx.include

server {
    listen example.com;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen example.com;
    server_name example.com;
    root /home/$host;

    location = /favicon.ico {
        empty_gif;
    }

    location / {
        fastcgi_pass unix:/var/run/php5-fpm.sock; #debian default
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}
ln -s /home/example.com/nginx.include /etc/nginx/conf.d/example.com.conf

放入一些HTML

/home/example.com/template/landing/index.html.twig

/home/example.com/template/landing/some_page.html.twig
/home/example.com/template/landing/some_dir/some_other_page.html.twig
... and so on

重启nginx

如果需要,编辑本地机器上的/etc/hosts

访问

http://example.com/

http://example.com/some_page/
http://example.com/some_dir/some_other_page/
... and so on