yama/nodeinphp

A PHP wrapper around Node JS.

0.5 2023-09-14 02:22 UTC

This package is auto-updated.

Last update: 2024-04-14 03:28:25 UTC


README

The PHP instance of Project-Level Node JS.

This library allows you to install node js on your project making it possible for you to use node js even when it is not installed in the system. Moreover, You can consume the power of node js, npm and npx commands with ease using a php class.

This project is a development of the THIS project

Installation

NodePHP is available on Packagist and installation via Composer is the recommended way to install NodePHP.

composer require yama/nodeinphp

Examples

<?php
require_once "vendor/autoload.php";
$node = new \Yama\NodeInPhp\Node(__DIR__);
$node->nvmUse('16.20.0');

if ($node->packagesExists() && !$node->packagesInstalled()) {
    $response = $node->installPackages();

    if ($response->statusCode() == '0') {
        echo "Packages successfully installed.";
    } else {
        echo "Failed to install the packages.";
    }

    print_r($response->output());
}

$response = $node->node('-v');

$message = 'Node Version: ' . PHP_EOL;
foreach ($response->output() as $line) {
    $message .= $line . PHP_EOL;
}
echo $message . PHP_EOL;

$response = $node->npm('-v');

$message = 'NPM Version: ' . PHP_EOL;
foreach ($response->output() as $line) {
    $message .= $line . PHP_EOL;
}
echo $message . PHP_EOL;

$response = $node->npx('-v');

$message = 'NPX Version: ' . PHP_EOL;
foreach ($response->output() as $line) {
    $message .= $line . PHP_EOL;
}
echo $message . PHP_EOL;

Credits

License

The MIT License (MIT). Please see License File for more information.