2012-05-22 64 views
0

我試圖將Evernote SDK集成到我的CodeIgniter Web應用程序中,並且庫中的某些類已加載,而其他類未加載:-S我看不到爲什麼。爲什麼有些類沒有從我的庫上加載CodeIgniter

我有一個簡單的代碼:

$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); 

雖然$userStoreTrans$userStoreProt正確創建,從Evernote的SDK一個THttpClientTBinaryProtocol類,$userStoreClient拋出一個Class 'UserStoreClient' not found in .../home.php

我不明白爲什麼有些類別是可以識別的,有些則不是,我可以看到的主要區別在於「TClasses」在evernote-sdk-php/lib/transport/*.phpevernote-sdk-php/lib/protocol/*.php之間,而UserStoreClient有一個額外的文件夾evernote-sdk-php/lib/packages/UserStore/*.php

我將解釋如何我包括Evernote的-SDK-PHP我笨安裝:

這是我笨的config/autoload.php

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

這是我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'); 
    } 
} 
?> 

Evernote_Bootstrap類的主要目的是require_once of evernote-sdk-php/lib/autoload.php,這個類是自動生成的使用-phpa舊貨發電機標誌,我只廣告d一些日誌記錄來查看問題。

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 <[email protected]> 
*/ 

/** 
* 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'); 
    } 
    } 
} 

然後我登錄了圖書館,似乎很好地工作。你可以看到主要的重要日誌:log_message('debug',str_var_dump($allLibs));log_message('debug','all scrapped classes: ' . str_var_dump($GLOBALS['THRIFT_AUTOLOAD']));和輸出:http://pastebin.com/8w1MCKx9

正如你所看到的,UserStore類似乎比THttpClient或TBinaryProtocol ......有關該問題的任何想法同樣精細裝?

我不知道是否重要,但我注意到在autoload.php中定義的$GLOBALS['THRIFT_ROOT']不能從CI控制器訪問。可能我錯過了CI架構的一些東西。

回答

0

從我們最新的SDK更新開始,UserStoreClient(和其他SDK類)位於適當的命名空間中。假設您正在使用我們生成的代碼,是否導入了您正在使用的類?例如。

use EDAM\UserStore\UserStoreClient; 
相關問題