我尝试开发一个类来安装或更新所有或一个或多个librray。但是我被阻止了。
我试图从这篇文章开始: Run composer with a PHP script in browser
我的结构
/Shop/
/Shop/composer.json
/Shop/composer.lock
/Shop/includes/External/Vendor
/Shop/includes/Core
注意:所有库都位于/Shop/includes/External/Vendor
我要制作的类允许我安装库或更新所有或一个库
namespace Site\SiteAdmin;
use Site\Core;
use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
class Composer {
const EXTRACT_DIRECTORY = BASE_DIR . 'External';
public static function ComposerUpdate($libray = null) {
if (is_null(libray) {
if (file_exists(static::EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
echo 'Extracted autoload already exists. Skipping phar extraction as presumably it\'s already extracted.';
} else{
$composerPhar = new Phar('Composer.phar');
//php.ini setting phar.readonly must be set to 0
$composerPhar->extractTo(static::EXTRACT_DIRECTORY);
}
//This requires the phar to have been extracted successfully.
require_once (static::EXTRACT_DIRECTORY . '/vendor/autoload.php');
chdir(Core::getConfig('dir_root', 'Shop'));
//Create the commands
$input = new ArrayInput(['command' => 'update']);
//Create the application and run it with the commands
$application = new Application();
try {
$application->setAutoExit(false); // prevent `$application->run` method from exitting the script
$application->run($input);
} catch (\Exception $e) {
var_dump($e);
}
} else {
// update libray like phpmailer for example
}
}
// how to make this function to install a specific library like phpmailer for example
public function ComposerInstall($library) {
if (file_exists(static::EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.";
} else{
$composerPhar = new Phar("Composer.phar");
//php.ini setting phar.readonly must be set to 0
$composerPhar->extractTo(static::EXTRACT_DIRECTORY);
}
//This requires the phar to have been extracted successfully.
require_once (static::EXTRACT_DIRECTORY.'/vendor/autoload.php');
// change out of the webroot so that the vendors file is not created in
// a place that will be visible to the intahwebz
chdir(Core::getConfig('dir_root', 'Shop'));
// call `composer install` command programmatically
$input = new ArrayInput(array('command' => 'install'));
$application = new Application();
$application->setAutoExit(false); // prevent `$application->run` method from exitting the script
$application->run($input);
echo "Done.";
}
}