Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarty5: installation without composer #999

Open
lifecom opened this issue Apr 18, 2024 · 8 comments
Open

Smarty5: installation without composer #999

lifecom opened this issue Apr 18, 2024 · 8 comments
Labels
waiting Waiting for answer

Comments

@lifecom
Copy link

lifecom commented Apr 18, 2024

Is there any way for installation Smarty 5 without composer?

@scottchiefbaker
Copy link

With Smarty v4.x there definitely was a way. Checking the docs and the source for v5.x it's not as clear. I would very interested in not using composer as well. If this gets figured out we should update the docs with this info.

@wisskid
Copy link
Contributor

wisskid commented Apr 19, 2024

This was discussed here
Does this solve your problem?

@wisskid wisskid added the waiting Waiting for answer label Apr 19, 2024
@scottchiefbaker
Copy link

That's pretty nebulous... I'd really like to see "official" documentation on how to do this. I'm not a huge fan of composer.

@wisskid
Copy link
Contributor

wisskid commented May 7, 2024

Okay. Every (well, almost every, I guess) composer.json file has this section called "autoload".

For Smarty it currently reads:

"autoload": {
        "psr-4" : {
            "Smarty\\" : "src/"
        },
        "files": [
            "src/functions.php"
        ]
    },

So, register a PSR-4 autoloader for the Smarty namespace that reads from the src folder and include src/functions.php once.

That should do it.

@scottchiefbaker
Copy link

For Smarty 4.x my instantiation is:

require("include/smarty/libs/Smarty.class.php");
$smarty = new Smarty();

I've been doing it that way for 8+ years on Smarty 3.x and 4.x. I'd like to do something similar with Smarty 5.x.

require("smarty-5.0.2/src/functions.php");

use Smarty\Smarty;
$smarty = new Smarty();

I get Fatal error: Uncaught Error: Class "Smarty\Smarty" not found in... What am I doing wrong? I'm not familiar with PSR-4 autoloaders.

@scottchiefbaker
Copy link

FWIW I'm 100% willing to write up some documentation and submit a PR for how load Smarty without composer once I get it working. Clearly there are other people that desire this functionality as well. It would be good to have it in the official documentation.

@wisskid
Copy link
Contributor

wisskid commented May 8, 2024

I asked ChatGPT to write it, seems like this might work:


spl_autoload_register(function ($class) {
    // Namespace prefix
    $prefix = 'Smarty\\';
    
    // Base directory for the namespace prefix
    $baseDir = __DIR__ . '/a/b/src/';
    
    // Does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // If not, move to the next registered autoloader
        return;
    }
    
    // Get the relative class name
    $relativeClass = substr($class, $len);
    
    // Replace namespace prefix with base directory, replace namespace separators with directory separators, and append .php
    $file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
    
    // If the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

If not, just run composer require smarty/smarty && composer dump-autoload and see what's in vendor/autoload.php

@joelmiguelvalente
Copy link

joelmiguelvalente commented May 12, 2024

El código brindado por @wisskid funciona bien, lo acabo de comprobar

<?php

define('SMARTY', __DIR__ . DIRECTORY_SEPARATOR . 'smarty' . DIRECTORY_SEPARATOR);

spl_autoload_register(function ($class) {
   // Namespace prefix
   $prefix = 'Smarty\\';
   
   // Base directory for the namespace prefix
   $baseDir = SMARTY;
   
   // Does the class use the namespace prefix?
   $len = strlen($prefix);
   if (strncmp($prefix, $class, $len) !== 0) return;

   // Get the relative class name
   $relativeClass = substr($class, $len);
   
   // Replace namespace prefix with base directory, replace namespace separators with directory separators, and append .php
   $file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
   
   // If the file exists, require it
   if (file_exists($file)) require $file;
});

Pero en donde vayan a utilizar $smarty = new Smarty();, debe ser así, como lo menciona en su documentación

use Smarty\Smarty;
$smarty = new Smarty();

ya que había intentado poner use Smarty\Smarty; en autoload.php para usarlo directamente con la variable, pero da error

Fatal error: Uncaught Error: Class "Smarty" not found in...

PD: Ahora resulta que tengo que realizar cambios para adaptar los plugins que había realizado y la forma de agregar otra carpeta de plugins es otra, la verdad prefiero la versión Smarty 4.5.1, para mí, era más fácil y simple de usar... Esta versión tiene muchos cambios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting Waiting for answer
Projects
None yet
Development

No branches or pull requests

4 participants