T
T
tripcollor2019-01-25 13:22:20
PHP
tripcollor, 2019-01-25 13:22:20

How to run an external program from a package in composer that is located in the directory of the package itself?

I'm trying to make my own composer package, based on a plugin in github. Here it is https://github.com/chapagain/php-swiss-ephemeris
There is one caveat here:
the php script uses and runs the external Swiss Ephemeris program in this plugin that I rely on it is located in the sweph folder.
I want to make my own composer package that would run an external program inside, receive data from it, then standard data processing would go on and bring it into the form that I need.
I currently have the following file structure:
src
--sweph
--Natal.php
vendor
composer.json
index.php Composer.json
content:

{
  "name": "tripcollor/astro",
  "description": "PHP library for calculations astro",
  "keywords": [
      "astrology"
  ],
  "authors": [
      {
          "name": "tripcollor",
          "email": "[email protected]"
      }
  ],
  "require": {
      "php": ">=5.6"
  },
  "require-dev": {
  },
  "autoload": {
      "psr-4": {
          "Astrology\\": "src"
      }
  }
}

I want to be able to include use Astrology\Natal in index.php and use some function, and the included file src/Natal.php, in turn, should call an external program inside itself, which is located next to it in the same directory in the sweph folder. This is how it should look like:
src/Natal.php file code
<?php

namespace Astrology;

use DateTime;

class Natal
{
  public function test()
  {
    $libPath = './sweph/';
    putenv("PATH=$libPath");
    $birthDate = new DateTime("1990-06-03T13:30:00Z");
    $latitude = 55.5557;
    $longitude = 52.5752;
    $timezone = 0; 
    $timezone * (60 * 60);
    $birthTimestamp = strtotime($birthDate->format('Y-m-d H:i:s'));
    $utcTimestamp = $birthTimestamp - $offset;
    $date = date('d.m.Y', $utcTimestamp);
    $time = date('H:i:s', $utcTimestamp);

    exec ("swetest -edir$libPath -b$date -ut$time -p0123456789ABCDEFGHIJKLMNOPQRSTUVWX -xz  -emos -fPls -g, -head", $output_t);
    
    print_r($output_t);
  }
}

We connect the index.php file code here and use our plugin:
<?php 

require './vendor/autoload.php';

use Astrology\Natal;

$Natal= new Natal();
$Natal->test();

It doesn't seem to work that way because $libPath = './sweph/'; accesses this folder not from the src/Natal.php file as I need, but from the file in which I include it, that is, in Index.php. What needs to be written in $libPath so that everything works as intended, and so that it works when loading a package into another project from composer. Of course, you can move the sweph folder to the root of the site and pass the path to it here $Ayurveda->test('./path/sweph/'); regarding the Index.php file for this example, but in the end I want everything to be done under the hood. Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hOtRush, 2019-01-25
@tripcollor

The structure is not quite correct.
- index.php - strange file for package composer. What function does it perform?
- Your program should not be in the source folder - you need to either move it to bin, or pull it up with a separate package, or just pass the absolute path to the binary in new Ayurveda($binPath)
- Well, ./swe .. is a bit strange, you need use something like realpath(dirname(__FILE__))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question