定制 brightnucleus/namespace-backtracer 二次开发

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

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

brightnucleus/namespace-backtracer

Composer 安装命令:

composer require brightnucleus/namespace-backtracer

包简介

Get the namespace of the calling object.

README 文档

README

Scrutinizer Code Quality Code Coverage Build Status Codacy Badge Code Climate

Latest Stable Version Total Downloads Latest Unstable Version License

Get the namespace of the calling object, by scanning the debug backtrace, skipping a known set of namespaces & functions in the process.

Table Of Contents

Overview

This class/trait allows you to traverse the debug_backtrace() to find out what the calling class/function or the calling namespace is. This is useful in cases where you need to find out at runtime in what context a given method was called.

Installation

The best way to install this package is through Composer:

composer require brightnucleus/namespace-backtracer

Basic Usage

Setting Things Up

To create a new class that can fetch the caller's name or namespace, you can either extend the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerClass class or, should you already have a class you need to extend, you can import the BrightNucleus\NamespaceBacktracer\NamespaceBacktracerTrait trait.

In both cases you'll want to override one or more of the three methods that allow you to adapt the behavior to your environment:

  • getIgnoredInterfaces()

This gets the list of interfaces/classes to ignore while traversing the backtrace.

  • getIgnoredFunctions()

This gets the list of functions to ignore while traversing the backtrace.

  • getGlobalNamespace()

This defines by what string the global namespace is represented.

Making The Call

To get the caller's namespace for a specific call, you pass the output of debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) that was executed from within the callee to the getCallingNamespace() method. If you don't provide a debug_backtrace output, it will get fetched within the trait (which might be a different context than the one you want to check).

Example:

<?php

namespace CalleeNamespace {

    use BrightNucleus\NamespaceBacktracer\NamespaceBacktracerTrait;

    class Callee {

        use NamespaceBacktracerTrait;

        protected function getIgnoredInterfaces() {
            return [
                'CalleeNamespace\Callee',
            ];
        }

        public function calledFunctionGetCaller() {
            echo $this->getCaller();
        }

        public function calledFunctionGetNamespace() {
            echo $this->getCallingNamespace();
        }
    }

}

namespace CallerNamespace {

    use CalleeNamespace\Callee;

    class Caller {

        public function callingFunctionGetCaller() {
            $callee = new Callee();
            $callee->calledFunctionGetCaller();
        }

        public function callingFunctionGetNamespace() {
            $callee = new Callee();
            $callee->calledFunctionGetNamespace();
        }
    }
}

$caller = new CallerNamespace\Caller();

// This will echo "CallerNamespace\Caller" from within the
// CalleeNamespace\Callee\calledFunction() method.
$caller->callingFunctionGetCaller();

// This will echo "CallerNamespace" from within the
// CalleeNamespace\Callee\calledFunction() method.
$caller->callingFunctionGetNamespace();

Contributing

All feedback / bug reports / pull requests are welcome.

License

Copyright (c) 2016 Alain Schlesser, Bright Nucleus

This code is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固