Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugins): plugins #24

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions auto_pinpointed.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php declare(strict_types=1);

namespace pinpoint;
use pinpoint\Common\PinpointDriver;
namespace Pinpoint;

use Pinpoint\Common\PinpointDriver;
use Pinpoint\Plugins\PerRequestPlugins;
define('CLASS_PREFIX','Proxied_');

PinpointDriver::getInstance()->start();

if(defined('PP_REQ_PLUGINS') && class_exists(PP_REQ_PLUGINS)){
$plugins = PP_REQ_PLUGINS;
$plugins::instance();
}else{
PerRequestPlugins::instance();
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
],
"autoload": {
"psr-4": {
"pinpoint\\": "lib/pinpoint/"
"Pinpoint\\": "lib/Pinpoint/"
}
},
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "^7"
},
"require": {
"php": ">=7",
"nikic/php-parser": "^4.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Time: 11:36 AM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use PhpParser\Node;
use PhpParser\PrettyPrinter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
* Time: 10:33 AM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\NodeTraverser;
//use pinpoint\Common\GenRequiredBIFileHelper;
//use Pinpoint\Common\GenRequiredBIFileHelper;

class CodeVisitor extends NodeVisitorAbstract
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
* Time: 4:41 PM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use pinpoint\Common\ClassFile;
use Pinpoint\Common\ClassFile;
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter;
use PhpParser\Node;
use pinpoint\Common\PluginParser;
use Pinpoint\Common\PluginParser;

class GenOriginClassFileHelper extends ClassFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* Time: 10:28 AM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use pinpoint\Common\PluginParser;
use pinpoint\Common\ClassFile;
use Pinpoint\Common\PluginParser;
use Pinpoint\Common\ClassFile;

class GenProxiedClassFileHelper extends ClassFile
{
Expand Down Expand Up @@ -58,8 +58,7 @@ private function getRealNp($node)
return $node->toString();
}elseif ($node instanceof Node\Expr\Variable){ //#16 support new a variable
return $node->name;
}
else{ // Use namespace suggestion
}elseif($node instanceof Node\Name){ // Use namespace suggestion
$prefixNm = $node->getFirst();
if(isset($this->suffix_use[$prefixNm])){
$prefix = $this->suffix_use[$prefixNm];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;


class NamingConf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Time: 10:33 AM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use PhpParser\ParserFactory;
use PhpParser\NodeTraverser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
* Time: 5:14 PM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;


class PinpointDriver
{
protected static $instance;
protected $clAr;
private $iniFile='';
protected static $pluginDir = [PLUGINS_DIR."/AutoGen/",PLUGINS_DIR."/"]; //*Plugin.php
protected $clAr = [];
private $settingIni=PLUGINS_DIR."/setting.ini";
// user autoGen class and internal autoGen class
private static $autoGenDirs = [PLUGINS_DIR."/AutoGen/" , __DIR__."/../Plugins/AutoGen"]; //*Plugin.php

public static function getInstance(){

Expand All @@ -42,29 +43,26 @@ public static function getInstance(){

final private function __construct()
{
$this->clAr = [];
$this->iniFile = PLUGINS_DIR."/setting.ini";
}

public static function getAutoGenPlugins()
public static function getAutoGenFiles()
{
$pluFiles = [];
$files = [];

foreach (static::$pluginDir as $dir)
foreach (static::$autoGenDirs as $dir)
{
if(is_dir($dir))
{
Util::scanDir($dir,"/Plugin.php$/",$pluFiles);
break;
Util::scanDir($dir,"/Plugin.php$/",$files);
}
}
return $pluFiles;

return $files;
}

public function start()
{

VendorAdaptorClassLoader::init();
RenderAopClass::getInstance();
/// checking the cached file exist, if exist load it
if(Util::checkCacheReady())
Expand All @@ -73,18 +71,18 @@ public function start()
RenderAopClassLoader::start();
return ;
}

$pluFiles = static::getAutoGenPlugins();
VendorAdaptorClassLoader::enable();
$pluFiles = static::getAutoGenFiles();
foreach ($pluFiles as $file)
{
new PluginParser($file,$this->clAr);
}
/// there hidden a duplicated visit, class locates in @hook and appendFiles.
/// while, it's safe to visit a class/file in appendfiles and @hook order
$naming = [];
if(file_exists($this->iniFile))
if(file_exists($this->settingIni))
{
$nConf = new NamingConf($this->iniFile);
$nConf = new NamingConf($this->settingIni);
$naming = $nConf->getConf();

if(isset($naming['appendFiles']))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@
* Time: 10:29 AM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use PhpParser\ParserFactory;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitorAbstract;
use PhpParser\NodeTraverser;
use pinpoint\Common\PluginVisitor;


class PluginParser
Expand Down Expand Up @@ -110,23 +105,23 @@ public function run()
public function insertFunc($funcName, $mode)
{
$split = stripos ($funcName,'::');
$uCl = substr($funcName,0,$split);
$uFunc = substr($funcName,$split+2);
$clsFullName = substr($funcName,0,$split);
$methodName = substr($funcName,$split+2);

/// not Internal func
if(!array_key_exists($uCl,$this->clArray))
if(!array_key_exists($clsFullName,$this->clArray))
{
// Cl = APP\Foo
// func = open
$this->clArray[$uCl] = array( $uFunc =>
$this->clArray[$clsFullName] = array( $methodName =>
array($mode,$this->namespace,$this->className));
}elseif (!array_key_exists($uFunc,$this->clArray[$uCl]))
}elseif (!array_key_exists($methodName,$this->clArray[$clsFullName]))
{
$this->clArray[$uCl][$uFunc]= array($mode,$this->namespace,$this->className);
$this->clArray[$clsFullName][$methodName]= array($mode,$this->namespace,$this->className);
}
else {
// when user tears the plugins, that only works on $mode
$this->clArray[$uCl][$uFunc][0] |= $mode;
$this->clArray[$clsFullName][$methodName][0] |= $mode;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* Time: 2:37 PM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use pinpoint\Common\PluginParser;
use Pinpoint\Common\PluginParser;

class PluginVisitor extends NodeVisitorAbstract
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/


namespace pinpoint\Common;
namespace Pinpoint\Common;

class RenderAopClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Time: 2:53 PM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

class RenderAopClassLoader
{
Expand Down
4 changes: 2 additions & 2 deletions lib/pinpoint/Common/Util.php → lib/Pinpoint/Common/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Time: 4:21 PM
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

class Util
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public static function scanDir($dir,$pattern,&$tree)
if(is_dir($loc)){
static::scanDir($loc,$pattern,$tree);
}elseif (preg_match($pattern,$loc)){
$tree[]=$loc;
$tree[]=realpath($loc);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

namespace pinpoint\Common;
namespace Pinpoint\Common;

use Composer\Autoload\ClassLoader;
use Exception;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function loadClass($class)
* @param $classIndex
* @return bool
*/
public static function init()
public static function enable()
{
$loaders = spl_autoload_functions();
foreach ($loaders as &$loader) {
Expand Down
53 changes: 53 additions & 0 deletions lib/Pinpoint/Plugins/AutoGen/GuzzleHttp/GuzzlePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/******************************************************************************
* Copyright 2020 NAVER Corp. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
namespace Pinpoint\Plugins\AutoGen\GuzzleHttp;

use Pinpoint\Plugins\Common\Candy;
use Pinpoint\Plugins\Sys\curl\CurlUtil;

///@hook:GuzzleHttp\Client::request
class GuzzlePlugin extends Candy
{
///@hook:GuzzleHttp\Psr7\Request::__construct
function onBefore()
{
if(strpos($this->apId, "Request::__construct") !== false){
pinpoint_add_clue(PP_DESTINATION,CurlUtil::getHostFromURL($this->args[1]->getHost()));
pinpoint_add_clues(PP_HTTP_URL,$this->args[1]);
pinpoint_add_clue(PP_SERVER_TYPE,PP_PHP_REMOTE);

$n_headers =[] ;
if( is_array($this->args[2]) && array_key_exists('headers',$this->args[2]))
{
$n_headers = $this->args[2];
}
$n_headers = array_merge($n_headers,CurlUtil::getPPHeader($this->args[1]));
$this->args[2] = $n_headers;
}
}

function onEnd(&$ret)
{
pinpoint_add_clue(PP_NEXT_SPAN_ID,pinpoint_get_context(PP_NEXT_SPAN_ID));
pinpoint_add_clues(PP_HTTP_STATUS_CODE,(string)($ret->getStatusCode()));
}

function onException($e)
{
pinpoint_add_clue(PP_ADD_EXCEPTION,$e->getMessage());
}
}
Loading