Viewed   178 times

I need to use some PHP libraries with dependencies but I have some restrictions on the webserver of the client. It is a managed webserver and I can not use a console eg over SSH.

So how do I use now these libraries without Composer?
Can I create some directories manually and what directories or paths do I need to create? Also, what do I need to create so autoloading and namespacing is working?

Can I create the autoload.php somehow manually and what is the content of the file?

 Answers

4

It is possible with a simple autoloader and it is not so hard to do it:

function __autoload($className)
{
    $className = ltrim($className, '\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    // $fileName .= $className . '.php'; //sometimes you need a custom structure
    //require_once "library/class.php"; //or include a class manually
    require $fileName;

}

But sometimes you have to adjust the $fileName so it works with all libraries. It depends on the standard for autoloading and how the class names of the libraries are named. Sometimes you have to split the classname on _ and use the first element for the direcotry name and add this also to the class name. I had for example a second library with a class like Library_Parser but the structure was Library/library-parser.php.

The first library worked directly with the above code and all classes were automatically loaded.

The code was taken from http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ but I had to correct some code parts (additional underscores and backslashes). I have used the PSR-0 Standard solution.

PSR-4 version by https://.com/users/1740659/thibault:

function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpaths) {
        if (!is_array($classpaths)) {
            $classpaths = array($classpaths);
        }
        spl_autoload_register(function ($classname) use ($namespace, $classpaths, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\#", "/", $classname).".php";
                foreach ($classpaths as $classpath) {
                    $fullpath = $dir."/".$classpath."/$filename";
                    if (file_exists($fullpath)) {
                        include_once $fullpath;
                    }
                }
            }
        });
    }
}

loadPackage(__DIR__."/vendor/project");

new CompanyNamePackageNameTest();
Friday, September 9, 2022
 
qrystal
 
3

If you try to install a private library/package via the "repositories" node of composer.json you have to re-specify the "autoload" node as well apparently.

{
  "repositories": [{
    "type": "package",
    "package": {
      "name": "foo/bar",
      "version": "0.0.1",
      "source": {
        "url": "[email protected]:benjamin.carl/bar.git",
        "type": "git",
        "reference": "master"
      },
      "autoload": {
        "psr-0": {
        "Foo": "Framework/"
      }
    }
  }]
}

I just spent a couple of hours figuring this out. Good job, Composer!

Friday, October 28, 2022
5

just check if the URL has a "http://" then proceed
else add a "http://" by your script

<!DOCTYPE html>
<html>
<body>
<?php
$url = $_GET['url'];
print $url;
if (strpos($url,'http') === 0) { //found at position 0
    //ok
} else {

    if ((strpos($url,'google') === 0) or
       (strppos($url,'some.other.site1') === 0) or 
       (strppos($url,'some.other.site2') === 0))  // here all the knowns SSL - Sites (this can not be the ultimate solution)
    {
        $url = 'https://'.$url;
    } else {
        $url = 'http://'.$url;
    }

}
print '<br/>'.$url; 
?>

<form name="input" method="get">
Url: <input type="text" name="url" action="<?php echo $url; ?>">
<input type="submit" value="Go">
</form>

<iframe src="<?php echo $url; ?>">
  <p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
Wednesday, December 14, 2022
2

You can use the autoload mapping in order to adchieve that. Just create a folder with the file/files that you want to use. For example:

YourProject/app/MyClasses/

And store the files right there. Then you just have to use the composer classmap: Edit the composer.json file indicating the path to your classes:

"autoload": {
    ...
    "classmap": [
        "database/seeds",
        "database/factories"
        "app/MyClasses"
    ],
    ...
},

Run the composer dump-autoload command and you'll be ready to go.

Friday, September 30, 2022
 
4

You can autoload specific files by editing your composer.json file like this:

"autoload": {
    "files": ["src/helpers.php"]
}

(thanks Kint)

Friday, September 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :