Search code examples
phpfunctionclasscomposer-phppackagist

My composer project's class won't load


I have a Packagist package for composer, lwilson/onepage, with one PHP file. The file has one class, which contains one function. I have installed the package on my Web server, and I have a PHP file which is supposed to use it. I know that the composer autoloader is included properly, since I could use other composer libraries installed on the server. I get the following result:

Fatal error: Class 'OnePage\OnePage' not found in /home/photox952/public_html/onepage/test_onepage.php on line 6

test_onpepage.php (the file that uses the class):

<?php
require '../../vendor/autoload.php';

use OnePage\OnePage;

file_put_contents("index.php",OnePage::OnePage(json_decode(file_get_contents("cfg.json")),__DIR__));
?>

compiler.php (the file in the package):

<?php
namespace OnePage;

// This is licenced under the GNU GENERAL PUBLIC LICENSE.  More info at: https://github.com/LeoWilson-XnD/lwilson_onepage/blob/master/LICENSE

class OnePage{
	public static function OnePage($cfg,$dir){
		$tmplt_u = "<?php if(\$_REQUEST['type']==\"{TYPE}\"&&\$_REQUEST['src']==\"{SRC}\"){header(\"Content-Type: {CT}\"); ?>{DATA}<?php } ?>";
		$tmplt_c = "<?php if(\$_REQUEST['all']==\"{TYPE}\"){header(\"Content-Type: {CT}\"); ?>{DATA}<?php } ?>";
		$res = "<?php /* This code is generated by the OnePage tool.  Find out more here: https://github.com/LeoWilson-XnD/lwilson_onepage */ ?>";
		$dir.=$cfg['dir'];
		if($cfg['v']==1){
			foreach($cfg['unique'] as $ka => $va){
				foreach($cfg[$ka] as $kb => $vb){
					$u = $tmplt_u;
					$u=str_replace("{TYPE}",explode("/",$ka)[1],$u);$u=str_replace("{SRC}",$kb,$u);$u=str_replace("{CT}",$ka,$u);$u=str_replace("{DATA}",file_get_contents($dir.$vb),$u);
					$res.=$u;
				}
			}
			foreach($cfg['combine'] as $ka => $va){
				$ds = "";
				foreach($cfg[$ka] as $kb => $vb){
					$ds.=file_get_contents($dir.$vb);
				}
				$u = $tmplt_c;
				$u=str_replace("{TYPE}",explode("/",$ka)[1],$u);$u=str_replace("{CT}",$ka,$u);$u=str_replace("{DATA}",file_get_contents($dir.$vb),$u);
				$res.=$u;
			}
			foreach($cfg['links'] as $key => $val){
				$res = str_replace($val,$key,$res);
			}
		}
		return $res;
	}
}
?>

composer.json (the package's composer.json file):

{
    "name": "lwilson/onepage",
    "description": "This allows users to compile websites easily into one PHP file.",
    "authors": [
        {
            "name": "Leo Wilson",
            "email": "[email protected]",
			"homepage": "https://xlww.net/",
			"role": "Developer"
        }
    ],
    "minimum-stability": "stable",
	"version": "v1.0.0",
	"homepage": "https://github.com/LeoWilson-XnD/lwilson_onepage",
	"licence":"GPL-3.0",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "psr-4": {
            "": "/"
        }
    }
}

I apologize in advance if I'm missing something obvious.


Solution

  • I think you set the autoload wrong in composer. Try to create a folder named OnePage and put the file with the class in it. It must be named OnePage.php

    Then set autoload like this

      "autoload": {
        "psr-4": {
          "OnePage\\": "OnePage"
        }
      }
    

    then run composer update from the shell. Then try running your php file again.

    The OnePage.php file should be in folder /home/photox952/public_html/onepage/OnePage