Feature No Code Attached Yet
avatar yangweijie
yangweijie
27 Dec 2023

Is your feature request related to a problem? Please describe.

There is a way to easy running a web application in workerman to speed up without modiy to much files in app.
A package calling 'joanhey/adapterman'

After my trying, it not work normally.

Problems:

  1. url changes, the output not change.
  2. Session not work
  3. Debug = true errors.
  4. Administrator is another app.

Key config for runing:

  • Nginx_config
server {
    listen 80;
    server_name joomla5.cn;
    root D:/git/php/joomla_workerman;

    autoindex off;

    #301_START
    #301_END

    #HTTP_TO_HTTPS_START
    #HTTP_TO_HTTPS_END

    #SSL_START
    #SSL_END

location / {
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Connection "keep-alive";
    proxy_set_header UPGRADE_INSECURE_REQUESTS 1;
    proxy_http_version 1.1;
}

location ^~/administrator/ {
    proxy_pass http://127.0.0.1:8081/;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Connection "keep-alive";
    proxy_set_header UPGRADE_INSECURE_REQUESTS 1;
    proxy_http_version 1.1;
}


    #REWRITE_START 引用url重写规则
    #include vhosts/rewrite/blog3.local.conf;
    #REWRITE_END

    #PHP_START
	#PHP_END

    #PHPMYADMIN_START
	#PHPMYADMIN_END

    #DENY_FILES_START
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 403;
    }
    #DENY_FILES_END

    location ~ .+\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      1d;
        error_log nul;
        access_log off;
    }

    location ~ .+\.(js|css|svg|woff2)$
    {
        expires      1h;
        error_log nul;
        access_log off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    access_log logs/joomla5.cn.access.log;
    error_log logs/joomla5.cn.error.log;
}

Describe the solution you'd like

i want a proper right start script. Which in run function it will reset app initilizition, and other cached variables in memory or in classes propty.

Additional context

avatar yangweijie yangweijie - open - 27 Dec 2023
avatar joomla-cms-bot joomla-cms-bot - change - 27 Dec 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 27 Dec 2023
avatar yangweijie
yangweijie - comment - 27 Dec 2023

start.php

<?php

use Joomla\CMS\Input\Input as CMSInput;
use Joomla\CMS\WebAsset\WebAssetManager;
use Joomla\Event\DispatcherInterface;

define('_JEXEC', 1);
require_once __DIR__ . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
//global $container;
function run()
{
//    static $app;
    ob_start();
    flog(date('Y-m-d H:i:s'));
//    global $container;
    // Instantiate the application.
    $container = \Joomla\CMS\Factory::getContainer();

    /*
     * Alias the session service keys to the web session service as that is the primary session backend for this application
     *
     * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
     * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
     * deprecated to be removed when the class name alias is removed as well.
     */
    $container->alias('session.web', 'session.web.site')
        ->alias('session', 'session.web.site')
        ->alias('JSession', 'session.web.site')
        ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
        ->alias(\Joomla\Session\Session::class, 'session.web.site')
        ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
    $app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
    \Joomla\CMS\Factory::$application = $app;
//    if($app){
//
//    }else{
//        $app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
//        // Set the application as global app
//        \Joomla\CMS\Factory::$application = $app;
//    }

    // Execute the application.
    $app->execute();
//    $ret = ob_get_clean();
    $ret = $app->getBody();
//    \Joomla\CMS\Factory::$container = null;
//    \Joomla\CMS\Factory::$application = null;
    // 重新设置webAssetManager 来恢复 locked 为false;
    $webAssetManager = new WebAssetManager(\Joomla\CMS\Factory::getContainer()->get('webassetregistry'));
//    $app->setCacheControllerFactory(null);
    $app->getDocument()->setWebAssetManager($webAssetManager);
//    $container->set(CMSInput::class, null);
//    $container->set(\Joomla\CMS\Application\SiteApplication::class, null);
//    unset($container);
//    unset($app);
    return $ret;
}

server.php

<?php

error_reporting(E_ALL&!E_DEPRECATED);

require_once __DIR__.'/vendor/autoload.php';

use Adapterman\Adapterman;
use Workerman\Worker;

function trace($msg, $nl = PHP_EOL){
    fwrite(STDOUT, var_export($msg, true) . ($nl ? "\n" : null));
}
function flog($msg, $path = './log.log'){
    $msg = is_string($msg)? $msg: var_export($msg, true);
    file_put_contents($path, $msg.PHP_EOL, FILE_APPEND);
}

Adapterman::init();

$http_worker = new Worker('http://0.0.0.0:8080'); // or 127.0.0.1:8080, or localhost:9000
$http_worker->count = 1; // or any positive integer
$http_worker->name = 'joomla'; // or any string

$http_worker->onWorkerStart = static function () {
    require __DIR__.'/start.php';
};

$http_worker->onMessage = static function ($connection, $request) {
    $request_uri = $request['server']['REQUEST_URI'];
//    flog($request_uri);
//    flog($request_uri);
//    if(str_contains($request_uri, 'media')){
//        $file = '.'.$request_uri;
//        $file = strstr($file, '?', true);
//        $file = realpath($file);
//        if(is_file($file)){
//            $response = new \Workerman\Protocols\Http\Response();
////            $response->header('Content-Type', 'text/css');
//            $response->withBody(file_get_contents($file));
////            header('Content-Type', mime_content_type($file));
//            return $connection->send($response);
//        }
//    }
    $connection->send(run());
};

Worker::runAll();

run.bat

php82 -c D:/phpEnv/php/php-8.2/php_disabled.ini server.php start
avatar yangweijie
yangweijie - comment - 27 Dec 2023

php_disable.ini

auto_globals_jit = Off

disable_functions=header,header_remove,headers_sent,http_response_code,setcookie,session_create_id,session_id,session_name,session_save_path,session_status,session_start,session_write_close,session_regenerate_id,set_time_limit
avatar yangweijie
yangweijie - comment - 27 Dec 2023

server_admin.php

<?php

error_reporting(E_ALL&!E_DEPRECATED);

require_once __DIR__.'/libraries/vendor/autoload.php';

use Adapterman\Adapterman;
use Workerman\Worker;

function trace($msg, $nl = PHP_EOL){
    fwrite(STDOUT, var_export($msg, true) . ($nl ? "\n" : null));
}
function flog($msg, $path = './log_admin.log'){
    $msg = is_string($msg)? $msg: var_export($msg, true);
    file_put_contents($path, $msg.PHP_EOL, FILE_APPEND);
}

Adapterman::init();

$http_worker = new Worker('http://0.0.0.0:8081'); // or 127.0.0.1:8080, or localhost:9000
$http_worker->count = 8; // or any positive integer
$http_worker->name = 'joomla admin'; // or any string

$http_worker->onWorkerStart = static function () {
    require __DIR__.'/start_admin.php';
};

$http_worker->onMessage = static function ($connection, $request) {
    $request_uri = $request['server']['REQUEST_URI'];
    flog($request_uri);
//    flog($request_uri);
//    if(str_contains($request_uri, 'media')){
//        $file = '.'.$request_uri;
//        $file = strstr($file, '?', true);
//        $file = realpath($file);
//        if(is_file($file)){
//            $response = new \Workerman\Protocols\Http\Response();
////            $response->header('Content-Type', 'text/css');
//            $response->withBody(file_get_contents($file));
////            header('Content-Type', mime_content_type($file));
//            return $connection->send($response);
//        }
//    }
    $connection->send(run_admin());
};

Worker::runAll();

start_admin.php

<?php

use Joomla\CMS\WebAsset\WebAssetManager;

define('_JEXEC', 1);

function datetime(){
    return date('Y-m-d H:i:s', time());
}

// Define JPATH constants if not defined yet
\defined('JPATH_BASE') || \define('JPATH_BASE', __DIR__);
\defined('JPATH_ROOT') || \define('JPATH_ROOT', JPATH_BASE);
\defined('JPATH_SITE') || \define('JPATH_SITE', JPATH_ROOT);
\defined('JPATH_PUBLIC') || \define('JPATH_PUBLIC', JPATH_ROOT);
\defined('JPATH_CONFIGURATION') || \define('JPATH_CONFIGURATION', JPATH_ROOT);
\defined('JPATH_ADMINISTRATOR') || \define('JPATH_ADMINISTRATOR', JPATH_ROOT . DIRECTORY_SEPARATOR . 'administrator');
\defined('JPATH_LIBRARIES') || \define('JPATH_LIBRARIES', JPATH_ROOT . DIRECTORY_SEPARATOR . 'libraries');
\defined('JPATH_PLUGINS') || \define('JPATH_PLUGINS', JPATH_ROOT . DIRECTORY_SEPARATOR . 'plugins');
\defined('JPATH_INSTALLATION') || \define('JPATH_INSTALLATION', JPATH_ROOT . DIRECTORY_SEPARATOR . 'installation');
\defined('JPATH_THEMES') || \define('JPATH_THEMES', JPATH_BASE . DIRECTORY_SEPARATOR .'administrator'.DIRECTORY_SEPARATOR. 'templates');
\defined('JPATH_CACHE') || \define('JPATH_CACHE', JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'cache');
\defined('JPATH_MANIFESTS') || \define('JPATH_MANIFESTS', JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'manifests');
\defined('JPATH_API') || \define('JPATH_API', JPATH_ROOT . DIRECTORY_SEPARATOR . 'api');
\defined('JPATH_CLI') || \define('JPATH_CLI', JPATH_ROOT . DIRECTORY_SEPARATOR . 'cli');

require_once JPATH_ADMINISTRATOR . '/includes/framework.php';

function run_admin(){
    ob_start();

    // Boot the DI container
    $container = \Joomla\CMS\Factory::getContainer();
    $container->alias('session.web', 'session.web.administrator')
        ->alias('session', 'session.web.administrator')
        ->alias('JSession', 'session.web.administrator')
        ->alias(\Joomla\CMS\Session\Session::class, 'session.web.administrator')
        ->alias(\Joomla\Session\Session::class, 'session.web.administrator')
        ->alias(\Joomla\Session\SessionInterface::class, 'session.web.administrator');

    trace($_SERVER);
//    $_SERVER['SERVER_NAME'] = 'joomla.cn';
//    $_SERVER['HTTP_HOST'] = 'joomla.cn';
//    if($_SERVER['REQUEST_URI'] == 'index.php'){
//        $_SERVER['REQUEST_URI'] = '/';
//    }else{
//        $_SERVER['REQUEST_URI'] = str_ireplace('administrator/index.php', '', $_SERVER['REQUEST_URI']);
//    }

//    flog(datetime());
    /*
     * Alias the session service keys to the web session service as that is the primary session backend for this application
     *
     * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
     * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
     * deprecated to be removed when the class name alias is removed as well.
     */
    // Instantiate the application.
    // Instantiate the application.
    $app = $container->get(\Joomla\CMS\Application\AdministratorApplication::class);

    // Set the application as global app
    \Joomla\CMS\Factory::$application = $app;

    // Set the application as global app
    // Saves the start time and memory usage.

    // Execute the application.
    $app->execute();
    $ret = $app->getBody();
    \Joomla\CMS\Factory::$container = null;
    \Joomla\CMS\Factory::$application = null;
    // 重新设置webAssetManager 来恢复 locked 为false;
    $webAssetManager = new WebAssetManager(\Joomla\CMS\Factory::getContainer()->get('webassetregistry'));
    $app->getDocument()->setWebAssetManager($webAssetManager);
    return $ret;
}

run_admin.bat

php -c C:/GreenSoft/EasySrv/core/software/php/php-8.2/php_disable.ini server_admin.php start
avatar richard67 richard67 - change - 27 Dec 2023
Labels Added: Feature
avatar richard67 richard67 - labeled - 27 Dec 2023
avatar rdeutz
rdeutz - comment - 3 Jan 2024

Joomla is not a strictly separated front/backend application I have my doubts that you will get this running.

avatar Hackwar Hackwar - change - 15 Feb 2024
Status New Closed
Closed_Date 0000-00-00 00:00:00 2024-02-15 09:03:35
Closed_By Hackwar
avatar Hackwar Hackwar - close - 15 Feb 2024
avatar Hackwar
Hackwar - comment - 15 Feb 2024

I've been thinking about this issue for several days and would really like to help you, but it already starts with the problem that I can't really find anything usefull about adaptman. And as Robert wrote, Joomla is not made for this, so I, too, fear that this wont be possible. Since I don't think we can provide support for this, I'm going to close this ticket. This isn't really a bug in Joomla, so not fitting for our bug tracker. But maybe you can get help in the Joomla forum at forum.joomla.org?

Add a Comment

Login with GitHub to post a comment