Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPWord Installation without composer

Tags:

php

phpword

Forgive the seeming lameness here. I'm working with a shared hosting with onnly access to cpanel and host are not willing to install composer nor PHPWord on my behalf. Is it possible to install PHPWord by just downloading the ZIP from github? Thanks

like image 803
Ojchris Avatar asked Jul 18 '17 15:07

Ojchris


People also ask

How do I install PHPWord?

PHPWord is installed via Composer. You just need to add dependency on PHPWord into your package. If you are a developer or if you want to help us with testing then fetch the latest branch for developers. Notice: all contributions must be done against the developer branch.


2 Answers

Yes, I do it all the time. For PHPWord, I would download the zip file from GitHub and move the contents of the src folder to a directory called "lib\PhpOffice\PhpWord". You'll then need a PHP class loader. I always use this for autoloading, provided the Classes are properly namespaced, which appears to be the case.

$GLOBALS['class_path'] = array(__DIR__ . '/lib', __DIR__);

// Set-up class_path superglobal variable using php include_path as basis
if (!array_key_exists('class_path', $GLOBALS)) {
    $GLOBALS['class_path'] = array();
    foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
        // substitute __DIR__ path for '.' instead
        if ($path == '.') {
            array_push( $GLOBALS['class_path'], realpath(__DIR__) );
            continue;
        }
        array_push( $GLOBALS['class_path'], realpath($path) );
    }
}

if (!function_exists('import')):
function import($package = '') {
    if (empty($package)) {
        trigger_error("Package path must be specified.", E_USER_ERROR);
    }
    $package_bits = explode('\\', $package);
    $package_path = implode(DIRECTORY_SEPARATOR, $package_bits) . '.php';
    foreach ($GLOBALS['class_path'] as $path) {
        $file = $path . DIRECTORY_SEPARATOR . $package_path;
        if (file_exists($file)) {
            require_once($file);
            $entity_name = implode('\\', $package_bits);
            if (!(class_exists($entity_name, false) ||
                interface_exists($entity_name, false)
                || trait_exists($entity_name, false))) {
            $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
            trigger_error("Entity '" . $package . "' not found in file '" . $package_path . "' for import called in " .
                    $caller['file'] . " on line " . $caller['line'], E_USER_ERROR);
            }
            return;
        }
    }
}
endif;

spl_autoload_register('import');

Set your $GLOBALS['class_path'] to the location of the 'lib' directory and be sure to use 'use' as needed before attempting to instantiate PHPWord.

Hope this helps!

like image 190
Borgboy Avatar answered Oct 19 '22 18:10

Borgboy


You can use Composer locally and then upload the files to the server.

I asked about this here: Using Composer locally then uploading files through FTP

Basically, if you can't run Composer on the server, use it locally, then copy the necessary files to the server.

The reason you should use it is because it will download the dependencies for you, without you having to manually configure a load of stuff. That's very much the point of Composer :)

composer require phpoffice/phpword

Using version ^0.13.0 for phpoffice/phpword

./composer.json has been created

Loading composer repositories with package information
Updating dependencies (including require-dev)

  - Installing pclzip/pclzip (2.8.2)
    Downloading: 100%         

  - Installing phpoffice/common (v0.2.6)
    Downloading: 100%         

  - Installing zendframework/zend-stdlib (2.4.13)
    Downloading: 100%         

  - Installing zendframework/zend-validator (2.4.13)
    Downloading: 100%         

  - Installing zendframework/zend-escaper (2.4.13)
    Downloading: 100%         

  - Installing phpoffice/phpword (v0.13.0)
    Downloading: 100%   
like image 3
Andy Avatar answered Oct 19 '22 17:10

Andy