定制 gubnota/smart_loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gubnota/smart_loader

Composer 安装命令:

composer require gubnota/smart_loader

包简介

More srightforward way to call pseudostatically without create every time in every new file new class instance

README 文档

README

Gubnota/smart_loader (aka g:: or \Gubnota\Gubnota) is a class- helper to load and reuse class instances. It makes re-using class instances easier throughout all application (no need to re-initialize new instance, each instance stored in global object). Autoload class helper increases drastically perfomance and convenience by reusing instances when calling class methods in pseudo-static way:

<?php
g::Class('method','arg',...); //or
g::instance()->Class->method('arg',...);
?>

You can by-pass variables to the other calee (method) in jQuery-way. By default, g:: stores only one class instace and re-use it when calling the same class from other place. Autoloader can be used two ways: composer pakagist default psr-4 autoloader, and g:: own autoloader. Fully compatible with >= PHP 5.4, PHP 7.0.

Installing by composer

Add gubnota/smart_loader string to your composer.json require section and run composer update:

    "require": {
        "php" : ">=5.4",
        "gubnota/smart_loader": "dev-master"
    },

Alternatively you can download package to include according to your autoloading pattern subsystem.

<?php
include('smart_loader-master/init.php');
?>

Included files

  • test.php - Example of usage
  • g.php - class name shortcode (call g:: rather than Gubnota\Gubnota)
  • Gubnota/Gubnota.php - main class helper that do all the stuff
  • John/Doe.php - class for calling (with only one public method)
  • init.php - include this to autoload

Autoloading classes

If you are not gonna use Composer psr-4 standard autoloading class solution, please don't forget to setup your own:

<?php
include(__DIR__.'/vendor/gubnota/smart_loader/init.php');
?>

You might want to use more advanced usage by adding extra directories to search:

<?php
include(__DIR__.'/vendor/gubnota/smart_loader/Gubnota/Smart_loader.php');
$l = \Gubnota\Smart_loader::instance();
$l->place('name_for_new_place_to_look_for',__DIR__);
spl_autoload_register([$l, 'load']);
?>

When you call smart_autoloader from anywhere of your other places:

Remapping long names

smart_loader also able to remap long class names to the short ones:

<?php
g::instance()->facade('john','John\Doe')->get_instance('john');
?>

See also at Facades section.

Usage

Creating instance of the class for calling only one method:

<?php
$john_doe = new John\Doe(); 
$john_doe->write_message_to('Samuel Smitters');
?>

That usually become a headache: you have to create every time in every new file utilizing class that class method. Old method includes utilize static method or creating the global variable. New apporach included in Laravel 4, for instance, using catching name method to create instance of, save every called instance of the class to special array to re-call it again without recreating. In most cases you only need this appoach.

Below several equal ways to initialize and call same class method with help of Gubnota/Gubnota loder:

<?php
\Gubnota\Gubnota::John('write_message_to', 'Laura Smith', 'Are you still there?');
?>

or:

<?php
\Gubnota\Gubnota::instance()->John->write_message_to('Dude', 'Got message?');
?>

or:

<?php
\Gubnota\Gubnota::instance()->John->write_message_to('Dude II', 'Got message?');
?>

or:

<?php
g::{'John\\Doe'}('write_message_to',
    ['Laura Smith','John Smith','John Marcus','Mark Brown'][rand(0,3)],
    ['How long does it take to reach your house?','How about we meet next Tuesday?','Hello there.','Are you still there?'][rand(0,3)]);
?>

or:

<?php
g::instance()->{'John\\Doe'}->write_message_to(
    ['Laura Smith','John Smith','John Marcus','Mark Brown'][rand(0,3)],
    ['How long does it take to reach your house?','How about we meet next Tuesday?','Hello there.','Are you still there?'][rand(0,3)]);
?>

or:

<?php
$instance = \Gubnota\Gubnota::instance();
$instance->John->write_message_to('Dude III', 'Got message?');
?>

Because individual instances is not creating every tame, it all shares same public porperty with random number in it. Any special case required re-creating another instance can be also done easily utilizing the new keyword:

<?php
print "g::instance()->rand = ".g::instance()->rand."\n";
print "\Gubnota\Gubnota::instance()->rand = ".$instance->rand." equals\n";
print "Creation of new independent pocket of classes:\n";
$instance2 = new \Gubnota\Gubnota();
print "\$instance2->rand = $instance2->rand not equals\n";
print "\Gubnota\Gubnota::instance()->rand = ".$instance->rand." still equals\n";
?>

Facades

For using name with full namespaces rather than write in every file, better to regsiter it with facades mechanism:

<?php
g::instance()
->empty_facade() // empty all facades default name values
->facade('John','John\Doe') // add default one value included with package
->facade('Smith','John\Doe') // facades can share same class among different names
->delete_facade('Smith') // deletes facade name called 'Smith'
;
?>

After you register facade, just call it anywhere like:

<?php
g::instance()
->facade('sw_sendmail','\Swift_SendmailTransport')
->facade('sw_smtp','\Swift_SmtpTransport')
->facade('sw_message','\Swift_Message')
->facade('sw_malier','\Swift_Mailer')
g::sw_message;
?>

Installing with composer

To be able to use within your composer-powered project, you need to install them via composer. Simply add the libraries to your project's composer.json then run php composer.phar install:

{
    "require": {
        "php" : ">=5.4",
        "gubnota/smart_loader": "dev-master"
    }
}

Config and runtime config

Currently the Gubnota\smart_loader or g symlink still lack a lot of config options they should probably accept.

Troubleshooting

If autoloader not works be sure to inlude in composer.json parent project file autoload and config directives like:

<?php
{
    "name": "gubnota/your_personal_project",
    "authors": [
        {
            "name": "Vladislav Muravyev",
            "email": "me@gubnota.ru"
        }
    ],
    "require": {
        "php" : ">=5.4",
    	"gubnota/smart_loader": "dev-master"
    },
    "min-stability":"dev-master",
    "config": {
        "vendor-dir": "./vendor/"
    },
    "autoload": {
        "psr-4": {"": ["app/"]}
    }
}

You can also autoload files explicily, like:

    "autoload": {
        "files": ["Gubnota/Gubnota.php","g.php","John/Doe.php"]
    }

gubnota/smart_loader 适用场景与选型建议

gubnota/smart_loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「autoload」 「php」 「helper」 「laravel4」 「statically」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gubnota/smart_loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gubnota/smart_loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 gubnota/smart_loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-13