0

For a project i need to use JavaBridge 7.0.1 on a TomcatServer 7. Everything worked well (test.php for example) but after i had put my project inside i have the following error :

Etat HTTP 500 - java.lang.RuntimeException: PHP Fatal error: Class 'Presenter' not found in C:\Developpements\eclipse\workspace\neon_2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\JavaBridge\index.php on line 85

I can see that some PHP class are not loaded when some are...

Index.php

<?php
session_start();

// error reporting
ini_set('display_errors', true);
error_reporting(1);


// autoload dependencies automatically via magical composer autoload
require_once 'vendor/autoload.php';

// website configuration file
require_once 'boot.php';

// db configuration file
require_once 'config.php';

// flight framework
require 'core/flight/Flight.php';

// custom functions
require_once 'func.php';

// autoload classes
require_once('autoload.php');

// error logging
if ($config['log_errors']) {
    Flight::set('flight.log_errors', true);
    $logFile = fopen($config['log_path'] . 'applog.log', 'a+');

    Flight::map(
       'error',
       function (Exception $ex) use ($logFile) {
           $message = date('d-m-Y h:i:s') . PHP_EOL . $ex->getTraceAsString() . PHP_EOL . str_repeat(
                 '-',
                 80
              ) . PHP_EOL . PHP_EOL;

           fwrite($logFile, $message);
           fclose($logFile);
       }
    );
}

// set config
Flight::set('config', $config);

// view path
Flight::set('flight.views.path', 'app/views/');

// set base path variable to be used in setting css js files in views
$request = (array) Flight::request();
Flight::set('base', $request['base']);
Flight::set('controller', $request['url']);
Flight::set('lastSegment', end(explode('/', $request['url'])));

// connect configuration
$database = $_SESSION['db'] ? $_SESSION['db'] : $config['database_dbname'];

ORM::configure('pgsql:host=' . $config['database_host'] . ';port=5432;dbname=' . $database);
ORM::configure('username', $config['database_user']);
ORM::configure('password', $config['database_password']);
$db = ORM::get_db();

// enable query logging
ORM::configure('logging', true);

Flight::set('db', $db);
Flight::set('dbname', $database);

$stmt = $db->query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'");
$data = $stmt->fetchAll(PDO::FETCH_NUM);
$data = arrayFlatten($data);

// create table names json file
$json = array();
foreach ($data as $datakey => $datavalue) {
    $json[]['word'] = $datavalue;
}

@file_put_contents('tables.json', json_encode($json));

$tables = Presenter::listTables($data);
Flight::set('tables', $tables);

if (false !== strpos($_SERVER['REQUEST_URI'], '/table')) {
    $currentTableKey = array_search(Flight::get('lastSegment'), $data, true);
    unset($data[$currentTableKey]); // remove current table

    // make dropdown options
    Flight::set('tablesOptions', getOptions($data));
}

// get an array of databases
$stmt = $db->query("SHOW DATABASES");
$data = $stmt->fetchAll(PDO::FETCH_NUM);
$data = arrayFlatten($data);
Flight::set('databaseOptions', getOptions($data, true, null, $database));

// setup custom 404 page
Flight::map(
   'notFound',
   function () {
       //include 'errors/404.html';
       header("HTTP/1.0 404 Not Found");
       exit('404 Not Found');
   }
);

// set global variables
Flight::set('appname', $config['appname']);

///////// setup routes /////////////
require_once 'routes.php';

// auto-logout after inactivity for 10 minutes
timeoutLogout(60);

// flight now
Flight::start();

Here, for example, Flight.php is well loaded but presenter.php is not (it's loaded with require_once('autoload.php'))

autoload.php

<?php

// autoload classes from controllers/classes/presenters folders
function autoloadController($className) {
    if (false !== strpos($className, 'flight')) return;

    $className = strtolower($className);
    $filename = "app/controllers/" . $className . ".php";

    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadClass($className) {
    if (false !== strpos($className, 'flight')) return;

    $className = strtolower($className);
    $filename = "app/classes/" . $className . ".php";

    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadPresenter($className) {
    if (false !== strpos($className, 'flight')) return;

    $className = strtolower($className);
    $filename = "app/presenters/" . $className . ".php";

    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register('autoloadController');
spl_autoload_register('autoloadPresenter');
spl_autoload_register('autoloadClass');

This is weird because presenter.php is well located :

enter image description here

presenter.php

<?php

class Presenter
{
    public static function listTables(array $array)
    {
        $html = '';

        $base = Flight::get('base');

        $counter = 0;
        foreach ($array as $arrayitem) {
            $counter ++;

            $html .= <<< HTML
            <li><a href="$base/table/$arrayitem">$arrayitem</a></li>
HTML;
        }

        return $html;
    }

    public static function listTableData(array $array, $fieldTypes = array())
    {
        //$fieldTypes = convertFieldTypesEditable($fieldTypes);

        $html = '<table class="table table-striped table-bordered table-hover">' . "\n";
        $html .= '<thead>' . "\n";

        // build headings
        foreach ($array[0] as $head => $value) {
            $html .= "<th>$head</th>" . "\n";
        }

        $html .= '</thead>' . "\n";

        // build body
        $html .= '<tbody>' . "\n";

        //pretty_print($array);

        foreach ($array as $subArray) {
            $html .= '<tr>' . "\n";

            foreach ($subArray as $value) {
                $html .= '<td style="white-space: nowrap !important;">' . $value . '</td>' . "\n";
            }

            $html .= '</tr>' . "\n";
        }

        $html .= '</tbody>' . "\n";
        $html .= '</table>' . "\n";

        return $html;
    }
}

Finally, if a try this in Wamp, everything work perfectly

7
  • What's the location of Flight.php ? Commented Mar 21, 2017 at 13:49
  • @AhmadRezk , look at my edit Commented Mar 21, 2017 at 13:55
  • So, you said that flight is loaded well but presenter not. And as I see structure of directories of flight class not as the same as presenter Try to use absolute path here $filename = "app/presenters/" . $className . ".php"; don't start from app, start from root using __DIR__ Commented Mar 21, 2017 at 14:01
  • it doesn't work but if i add require_once 'app/presenters/presenter.php'; at the beginning it work. So maybe it's because spl_autoload_register cant work in Javabridge environment ? Commented Mar 21, 2017 at 14:10
  • are you sure that your program get into if (is_readable($filename)) { condition ? Commented Mar 21, 2017 at 14:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.