Google API Loader

HI,

I try to load with the Loader, the src of the Google API. But, it keeps telling me the he can't find the class Google_Client.

Fatal error: Class 'Google_FileCache' not found in /usr/local/zend/apache2/htdocs/vendor/google/src/Google_Client.php on line 94

This is my config file (By the way it works very find for the incubator :

'application' => array(

    'controllersDir' => __DIR__ . '/../../app/controllers/',
    'modelsDir'      => __DIR__ . '/../../app/models/',
    'viewsDir'       => __DIR__ . '/../../app/views/',
    'pluginsDir'     => __DIR__ . '/../../app/plugins/',
    'libraryDir'     => __DIR__ . '/../../app/library/',
    'incubator'      => __DIR__ . '/../../vendor/phalcon/incubator/Library/',
    'googleAPI'      => __DIR__ . '/../../vendor/google/src/'
),

This my code in the index.php :

$config = include(__DIR__ . "/../app/config/config.php");

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs( 

    array(

        $config->application->incubator,
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->pluginsDir,
        $config->application->googleAPI,
))->register();

This the code where I try using it :

public function googleAction() {

    $client = new Google_Client();
    $client->setApplicationName("");

    $client->setClientId('');
    $client->setClientSecret('');
    $client->setRedirectUri('');
    //$client->setDeveloperKey('');

    $oauth2 = new Google_Oauth2Service($client);

    $code = $this->request->get('code');

    if (isset($code)) {

        $client->authenticate($code);
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        return;
    }

    if (isset($_SESSION['token'])) {

        $client->setAccessToken($_SESSION['token']);
    }

    /*if (isset($_REQUEST['logout'])) {

        unset($_SESSION['token']);
        $client->revokeToken();
    }*/

    if ($client->getAccessToken()) {

        $user = $oauth2->userinfo->get();

        // These fields are currently filtered through the PHP sanitize filters.
        // See http://www.php.net/manual/en/filter.filters.sanitize.php
        $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
        $img = filter_var($user['picture'], FILTER_VALIDATE_URL);
        $personMarkup = "$email<div><img src='$img?sz=50'></div>";

        // The access token may have been updated lazily.
        $_SESSION['token'] = $client->getAccessToken();
    } else {

        $authUrl = $client->createAuthUrl();
    }

    $this->response->redirect($authUrl, true);
}

Does someone have an idea or a solution ?

Thank you !



97.7k

Is Google_Client.php located at /../../vendor/google/src/?

Try adding an events manager to the loader to check why the route is not being loaded: http://docs.phalcon.io/en/latest/reference/loader.html#autoloading-events



30.0k

Hi,

Thanks for the answer. Yes, the Google_Client is in the /../../vendor/google/src/

Here's the eventManager answer:

/usr/local/zend/apache2/htdocs/maestro/app/config/../../vendor/phalcon/incubator/Library/Security.php /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/controllers/Security.php /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/models/Security.php /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/plugins/Security.php

it's loon like the folder is not loaded. I don't know why.

Thanks !



97.7k

can you print_r($loader)? This to see the directories in the internal properties.



30.0k

Here's the output of print_r command :

Phalcon\Loader Object ( [_eventsManager:protected] => Phalcon\Events\Manager Object ( [_events:protected] => Array ( [loader] => Array ( [0] => Closure Object ( [parameter] => Array ( [$event] => [$loader] => ) ) ) ) [_collect:protected] => [_enablePriorities:protected] => [_responses:protected] => ) [_foundPath:protected] => [_checkedPath:protected] => [_prefixes:protected] => [_classes:protected] => [_extensions:protected] => Array ( [0] => php ) [_namespaces:protected] => [_directories:protected] => Array ( [0] => /usr/local/zend/apache2/htdocs/maestro/app/config/../../vendor/phalcon/incubator/Library/ [1] => /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/controllers/ [2] => /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/models/ [3] => /usr/local/zend/apache2/htdocs/maestro/app/config/../../app/plugins/ [4] => /usr/local/zend/apache2/htdocs/maestro/app/config/../../vendor/google/src/ ) [_registered:protected] => 1 )



37.8k

Hi , if anyone cant resolve that problem . i did like that;

First change Google_client.php to GoogleClient.php (and of course in file change class name to GoogleClient)

and on config file set the location and registerDirs

DIR . $config->application->googleAPI,

it will be solved



30.0k

Wow I will give it a try.

Thank you.



85
edited Jul '14

Hello,

I got almost same problem for lastest Google API:

Warning: require_once(Google/Auth/AssertionCredentials.php): failed to open stream: No such file or directory in /var/www/phalcon/test/vendor/google/src/Google/Client.php on line 18

Anyone know this issue?

edited Oct '14

Hello,

I got almost same problem for lastest Google API:

Warning: require_once(Google/Auth/AssertionCredentials.php): failed to open stream: No such file or directory in /var/www/phalcon/test/vendor/google/src/Google/Client.php on line 18

Anyone know this issue?


मैंने गूगल की संस्थापना निर्देशिका के प्रयोग से inlcude_path को public पद्धति ग्रंथ के index.php में निर्धारित किया। set_include_path(get_include_path() . PATH_SEPARATOR . "path/to/google-api-php-client/src");


I used Google Installation Guide to put include_path in index.php of the public folder. set_include_path(get_include_path() . PATH_SEPARATOR . "path/to/google-api-php-client/src");



85

Thanks for you reply,

I resolved it by this:

My project folders tree:

  • config
  • controllers
  • library
    • Social
      • Google
        • Auth
        • Cache
        • ...
      • Google.php

Set library folder for Phalcon\Loader()

Google.php

<?php

$path = DIR;

set_include_path($path . PATH_SEPARATOR . get_include_path());

class Google extends \Phalcon\Mvc\User\Component {

public function authenticate() {

}

}

Use it:

  • $google = New Google();
  • $google->authenticate();

Sorry, it's very hard to format the thing here.



2.5k
edited Nov '19

I have use phalcon version 3.4 developed a production eCommerce ready project, starting early 2019 and we had the same issue with this Google library which their developer is not going to rewrite the code to support PSR-4 so it is not going to work with phalcon loader which already remove the prefix loading function like it was in 2.x version so here is the work around:

We use composer just as a downloader and mapper but not using its loader, we use phalcon loader to load anything, and we only commit those vendor files that is actually used no license no tests no docs, as we use git to deploy to production.We don't use composer to build as there is no need to build when you using PHP (you like build too much then use java or dotnet or nodejs) composer is never going to be like npm or yarn it will not give you compressed source code but big code including their .git files and so many sample files and your production code will be so messy

So use phalcon loader only

  1. update composer.json file

    {
    ....
    "autoload": {
    "classmap": [
        "vendor/google/apiclient/src/Google",
        "vendor/google/apiclient-services/src/Google/Service"
    ]
    }
    }
  2. run command line

    php composer dump-autoload
  3. using phalcon loader to replace composer most important files from composer is in /vendor/composer/autoload_xxxx.php files this files are actually mapping files returning you as arrays there are 9000+ class names dump to the file for all the google api and services (google cloud services)
<?php
use Phalcon\Loader;
$loader = new Loader();
$vendorDir = dirname(__DIR__) . DS . 'vendor';
$classMapFile = $vendorDir . DS . 'composer' . DS . 'autoload_classmap.php';
if (file_exists($classMapFile)) {
    $classMap = include $classMapFile;
    $loader->registerClasses($classMap);
}
$loader->register();

similar mapping but might need to change the namespace one as composer using array not string

as all the files are exported to autoload_classmap.php you don't need to register dir or namespaces and it wont work anyway

You could test this solution by running something like

$classname = '\Google_Service_Oauth2';
if (class_exists($classname)) {
    print_r($classname . ' exist');
} else {
    print_r($classname . ' not exist');
}

Currently Google and Google_Service 2 big package are still PSR-0 but \Google\Auth is already PSR-4 so you don't need to dump that package. I have tested locally using phalcon loader vs composer loader without APCu cache is 10ms vs 30ms but keep in mind that my vendor folder is much msaller than normal vendor folder if you include all pacakge like Magento2 did your IDE will crash in no time