Jump to content
iwato

Matomo - PHP Direct (for Dummies)

Recommended Posts

NATURE:  This is part of a steadfast effort to hack through the thicket of PHP classes and namespaces of which the Matomo application consists.  It is not an endeavor for the weak or timid, as the thicket is formidable and the documentation obscure.

BACKGROUND:  In order to submit a proper report request to the Matomo application the values of certain variables must be known.  Although there are generators within the application that have been created to achieve this task, they are not easily accessible, as the thicket is, well, thick.  Please find below both the file and code that produces the error message provided.

The FILE:

<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @package Piwik
 */

use Piwik\ErrorHandler;
use Piwik\ExceptionHandler;
use Piwik\FrontController;

if (!defined('PIWIK_ENABLE_ERROR_HANDLER') || PIWIK_ENABLE_ERROR_HANDLER) {
    ErrorHandler::registerErrorHandler();
    ExceptionHandler::setUp();
}

FrontController::setUpSafeMode();

if (!defined('PIWIK_ENABLE_DISPATCH')) {
    define('PIWIK_ENABLE_DISPATCH', true);
}

if (PIWIK_ENABLE_DISPATCH) {
    $environment = new \Piwik\Application\Environment(null);
    $environment->init();

    $controller = FrontController::getInstance();

    try {
        $controller->init();
        $response = $controller->dispatch();

        if (!is_null($response)) {
            echo $response;
        }
    } catch (Exception $ex) {
        ExceptionHandler::dieWithHtmlErrorPage($ex);
    }
}

The CODE:

require_once ('../../../../nudge.online/_utilities/php/matomo/core/dispatch.php');

The ERROR MESSAGE:

Quote

[11-Jun-2018 18:10:36 UTC] PHP Fatal error:  Class 'Piwik\ErrorHandler' not found in /home/.../matomo/core/dispatch.php on line 16

LINE 16

ErrorHandler::registerErrorHandler();

Now, including the file ErrorHandler.php does not help.  Neither does a call to the static function registerErrorHandler() appear to make a difference.

Piwik\ErrorHandler::registerErrorHandler();

For the response is always the same

Quote

PHP Fatal error:  Class 'Piwik\ErrorHandler' not found in /home/.../matomo/core/dispatch.php on line 16

Any ideas?

Roddy

 

Share this post


Link to post
Share on other sites

It's just saying that the class has not been included or defined.

Share this post


Link to post
Share on other sites

So, in addition to including the file in which the class is defined and calling a public static function within the file with its name and namespace as I have done what is a likely reason for not finding the class?

Please allow me to try a different line of questioning.  Consider the following list of uses and the corresponding namespace taken from the ExceptionHandler.php file.  Must each file containing the respective listed class be included by PHP  in order for the  class  to become available, or will PHP find the listed classes on its own?

namespace Piwik;

use Exception;
use Piwik\API\Request;
use Piwik\API\ResponseBuilder;
use Piwik\Container\ContainerDoesNotExistException;
use Piwik\Plugins\CoreAdminHome\CustomLogo;

And, to take the matter one-step further must I include the additional uses included in each of the above, respective, class files in order to achieve my goal?  I ask this for there is hardly a file that does not contain its own unique set of uses.

Roddy

Edited by iwato

Share this post


Link to post
Share on other sites

So, in addition to including the file in which the class is defined and calling a public static function within the file with its name and namespace as I have done what is a likely reason for not finding the class?

The single reason for not finding a class is that it hasn't been defined.  Maybe you didn't include the definition, maybe the autoloader isn't looking in the right place, etc.  If you want to list which files have been included, you can do that to check:

http://us.php.net/manual/en/function.get-included-files.php

Must each file containing the respective listed class be included by PHP  in order for the  class  to become available, or will PHP find the listed classes on its own?

Each file must be included, but you can take advantage of autoloading to have PHP look for the file in a specific location when you try to instantiate or use the class.

And, to take the matter one-step further must I include the additional uses included in each of the above, respective, class files in order to achieve my goal?

Yes.  If PHP is going to use a class then it needs to be included and defined.  Use statements do not include anything, they tell PHP what you're trying to do.  You can also alias the classes to change how you refer to them.

use Piwik\Container\ContainerDoesNotExistException As NoContainer;
	...
	throw new NoContainer();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×