简体   繁体   English

为什么我的CodeIgniter库没有加载某些类

[英]Why some classes are not loaded from my libraries on CodeIgniter

I'm trying to integrate Evernote SDK to my CodeIgniter web application and some classes from the library are loaded and others not, :-SI can't see why. 我正在尝试将Evernote SDK集成到我的CodeIgniter Web应用程序中,并且从库中加载了某些类,而没有加载其他类,:-SI无法理解原因。

I have that simple piece of code: 我有一段简单的代码:

$access_token = 'my validated access token ';

// Get User Store
$userStoreTrans;
try{
    $userStoreTrans = new THttpClient(USER_STORE_HOST, USER_STORE_PORT, USER_STORE_URL, USER_STORE_PROTO);
}
catch(TTransportException $e)
{
    print $e->errorCode.' Message:'.$e->parameter;
}

$userStoreProt = new TBinaryProtocol($userStoreTrans);
$userStoreClient = new UserStoreClient($userStoreProt, $userStoreProt);

While $userStoreTrans and $userStoreProt are created correctly, a THttpClient and TBinaryProtocol classes from Evernote SDK, $userStoreClient throws a Class 'UserStoreClient' not found in .../home.php 虽然正确创建了$userStoreTrans$userStoreProtTHttpClient Evernote SDK中的THttpClientTBinaryProtocol类, $userStoreClient抛出了Class 'UserStoreClient' not found in .../home.phpClass 'UserStoreClient' not found in .../home.php

I don't understand why some classes are recognized and some others not, the main difference that I can see is that "TClasses" are under evernote-sdk-php/lib/transport/*.php and evernote-sdk-php/lib/protocol/*.php and UserStoreClient has an extra folder evernote-sdk-php/lib/packages/UserStore/*.php 我不明白为什么可以识别某些类而不能识别某些类,我可以看到的主要区别是“ TClasses”位于evernote-sdk-php/lib/transport/*.phpevernote-sdk-php/lib/protocol/*.php和UserStoreClient有一个额外的文件夹evernote-sdk-php/lib/packages/UserStore/*.php

I'll explain how I'm including evernote-sdk-php to my CodeIgniter installation: 我将说明如何在我的CodeIgniter安装中包括evernote-sdk-php:

This is my CodeIgniter config/autoload.php 这是我的CodeIgniter config / autoload.php

$autoload['libraries'] = array('database','session','form_validation','security','tank_auth','Evernote_bootstrap');

This is my Evernote_bootstrap.php file 这是我的Evernote_bootstrap.php文件

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

define("EVERNOTE_LIBS", dirname(__FILE__) . DIRECTORY_SEPARATOR . "evernote-sdk-php/lib");

// add ourselves to include path
ini_set("include_path", ini_get("include_path") . ":" . EVERNOTE_LIBS);

require_once("evernote-sdk-php/lib/autoload.php");
require_once("evernote-oauth/functions.php");

class Evernote_Bootstrap
{
    function __construct()
    {
        // log_message('debug','Evernote_Bootstrap');
    }
}
?>

The main purpose of Evernote_Bootstrap class is the require_once of evernote-sdk-php/lib/autoload.php , this class is auto-generated using the -phpa Thrift generator flag, I only add some logging to see the problem. Evernote_Bootstrap类的主要目的是evernote require_once of evernote-sdk-php/lib/autoload.phprequire_once of evernote-sdk-php/lib/autoload.php ,该类是使用-phpa Thrift生成器标志自动生成的,我只添加了一些日志记录即可查看问题。

autoload.php: autoload.php:

<?php

/**
 * Copyright (c) 2006- Facebook
 * Distributed under the Thrift Software License
 *
 * See accompanying file LICENSE or visit the Thrift site at:
 * http://developers.facebook.com/thrift/
 *
 * @package thrift
 * @author Mark Slee <mcslee@facebook.com>
 */

/**
 * Include this file if you wish to use autoload with your PHP generated Thrift
 * code. The generated code will *not* include any defined Thrift classes by
 * default, except for the service interfaces. The generated code will populate
 * values into $GLOBALS['THRIFT_AUTOLOAD'] which can be used by the autoload
 * method below. If you have your own autoload system already in place, rename your
 * __autoload function to something else and then do:
 * $GLOBALS['AUTOLOAD_HOOKS'][] = 'my_autoload_func';
 *
 * Generate this code using the -phpa Thrift generator flag.
 */

/**
 * This parses a given filename for classnames and populates
 * $GLOBALS['THRIFT_AUTOLOAD'] with key => value pairs
 * where key is lower-case'd classname and value is full path to containing file.
 *
 * @param String $filename Full path to the filename to parse
 */
function scrapeClasses($filename) {
  $fh = fopen($filename, "r");
  while ($line = fgets($fh)) {
    $matches = array();
    if ( preg_match("/^\s*class\s+([^\s]+)/", $line, $matches)) {
      if (count($matches) > 1)
        $GLOBALS['THRIFT_AUTOLOAD'][strtolower($matches[1])] = $filename;
    }
  }
}

function findFiles($dir, $pattern, &$finds) {
  if (! is_dir($dir))
    return;
  if (empty($pattern))
    $pattern = "/^[^\.][^\.]?$/";
  $files = scandir($dir);
  if (!empty($files)) {
    foreach ($files as $f) {
      if ($f == "." || $f == "..")
        continue;
      if ( is_file($dir . DIRECTORY_SEPARATOR . $f) && preg_match($pattern, $f)) {
        $finds[] = $dir . DIRECTORY_SEPARATOR . $f;
      } else if ( is_dir($dir . DIRECTORY_SEPARATOR . $f) && substr($f, 0, 1) != ".") {
        findFiles($dir . DIRECTORY_SEPARATOR . $f, $pattern, $finds);
      }
    }
  }
}
function str_var_dump($object)
  {
    ob_start();
    var_dump($object);
    $dump = ob_get_clean();
    return $dump;
  }


// require Thrift core
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "Thrift.php");

if (! isset($GLOBALS['THRIFT_ROOT']))
  $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__);

log_message('debug','bootstrap autoload.php is executing');

// stuff for managing autoloading of classes
$GLOBALS['THRIFT_AUTOLOAD'] = array();
$GLOBALS['AUTOLOAD_HOOKS'] = array();
$THRIFT_AUTOLOAD =& $GLOBALS['THRIFT_AUTOLOAD'];


// only populate if not done so already
if (empty($GLOBALS['THRIFT_AUTOLOAD'])) {
  //$allLibs = glob( dirname(__FILE__) . "/**/*.php");  // oh poor winblows users can't use glob recursively
  $allLibs = array();
  findFiles( dirname(__FILE__), "/\.php$/i", $allLibs);
  log_message('debug',str_var_dump($allLibs));
  if (!empty($allLibs)) {
    foreach ($allLibs as $libFile) {
      scrapeClasses($libFile);
    }
    log_message('debug','all scrapped classes: ' . str_var_dump($GLOBALS['THRIFT_AUTOLOAD']));
  }
}else{
  log_message('debug','$GLOBALS[THRIFT_AUTOLOAD] already defined');
}


// main autoloading
if (!function_exists('__autoload')) {
  function __autoload($class) {
    log_message('debug','__autoload');
    global $THRIFT_AUTOLOAD;
    $classl = strtolower($class);
    if (isset($THRIFT_AUTOLOAD[$classl])) {
      // log_message('debug','$THRIFT_AUTOLOAD[$classl] is set, do require_once');
      //include_once $GLOBALS['THRIFT_ROOT'].'/packages/'.$THRIFT_AUTOLOAD[$classl];
      require_once($THRIFT_AUTOLOAD[$classl]);
    } else if (!empty($GLOBALS['AUTOLOAD_HOOKS'])) {
      log_message('debug','$GLOBALS[AUTOLOAD_HOOKS]is no empty, lets foreach');
      foreach ($GLOBALS['AUTOLOAD_HOOKS'] as $hook) {
        // log_message('debug','iterate');
        $hook($class);
      }
    } else {
        log_message('debug','nothing to do');
    }
  }  
}

Then I logged that library and seems to work fine. 然后,我登录了该库,看起来工作正常。 You can see the main important logs: log_message('debug',str_var_dump($allLibs)); 您可以看到主要的重要日志: log_message('debug',str_var_dump($allLibs)); and log_message('debug','all scrapped classes: ' . str_var_dump($GLOBALS['THRIFT_AUTOLOAD'])); log_message('debug','all scrapped classes: ' . str_var_dump($GLOBALS['THRIFT_AUTOLOAD'])); and the output: http://pastebin.com/8w1MCKx9 : 和输出: http : //pastebin.com/8w1MCKx9

As you can see, UserStore class seems to be equally fine loaded than THttpClient or TBinaryProtocol... any idea about the problem? 如您所见,UserStore类似乎比THttpClient或TBinaryProtocol加载得还好...关于此问题的任何想法?

I don't know if is important but I notice that $GLOBALS['THRIFT_ROOT'] , defined on autoload.php, is not accesible from a CI Controller. 我不知道它是否重要,但是我注意到在autoload.php上定义的$GLOBALS['THRIFT_ROOT']无法从CI控制器访问。 Probably I'm missing something about CI architecture. 可能我缺少有关CI体系结构的信息。

As of our lastest SDK updates, UserStoreClient (and the other SDK classes) are in appropriate namespaces. 从我们最新的SDK更新开始,UserStoreClient(和其他SDK类)位于适当的名称空间中。 Assuming that you're using our generated code, have you imported the classes you're using? 假设您正在使用我们生成的代码,是否已导入正在使用的类? Eg 例如

use EDAM\UserStore\UserStoreClient;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM