Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Swiss Ephemeris Ffi Laravel Package

jayeshmepani/swiss-ephemeris-ffi

PHP 8.3+ FFI wrapper for the Swiss Ephemeris C library. Exposes all 106 public API functions with 1:1 constant/signature parity and zero abstraction. No swetest shelling; outputs verified for parity via PHPUnit against swetest.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require jayeshmepani/swiss-ephemeris-ffi
    

    Ensure your php.ini has ffi.enable=true and PHP 8.3+.

  2. Basic Usage:

    use SwissEph\FFI\SwissEphFFI;
    
    $sweph = new SwissEphFFI();
    $jd = $sweph->swe_julday(2000, 1, 1, 12.0, SwissEphFFI::SE_GREG_CAL);
    $xx = $sweph->getFFI()->new("double[6]");
    $serr = $sweph->getFFI()->new("char[256]");
    
    $result = $sweph->swe_calc_ut($jd, SwissEphFFI::SE_SUN, SwissEphFFI::SEFLG_SPEED, $xx, $serr);
    
  3. Laravel Facade:

    use SwissEph\FFI\Facades\SwissEph;
    
    $sunPos = SwissEph::swe_calc_ut($jd, SwissEph::SE_SUN, SwissEph::SEFLG_SPEED, $xx, $serr);
    

Where to Look First

  • API Reference: Documentation for function signatures and constants.
  • Laravel Setup: Check config/swiss-ephemeris.php for custom paths or ephemeris file locations.
  • Ephemeris Files: Verify storage/app/swisseph/ contains .se1 files (published via php artisan vendor:publish --provider="SwissEph\FFI\SwissEphFFIServiceProvider").

First Use Case

Calculate planetary positions for a horoscope:

$jd = SwissEph::swe_julday(1990, 5, 17, 12.0, SwissEph::SE_GREG_CAL);
$xx = SwissEph::getFFI()->new("double[6]");
$serr = SwissEph::getFFI()->new("char[256]");

$sunPos = SwissEph::swe_calc_ut($jd, SwissEph::SE_SUN, 0, $xx, $serr);
$moonPos = SwissEph::swe_calc_ut($jd, SwissEph::SE_MOON, 0, $xx, $serr);

Implementation Patterns

Core Workflows

  1. Initialization:

    • Use the facade for Laravel:
      SwissEph::setLibraryPath('/custom/path/to/libswisseph.so');
      
    • Singleton Note: Paths set after initialization may fail. Set paths before instantiating.
  2. Date Handling:

    • Convert Gregorian dates to Julian Day (JD) first:
      $jd = SwissEph::swe_julday($year, $month, $day, $hour, SwissEph::SE_GREG_CAL);
      
  3. Planetary Calculations:

    • Use swe_calc_ut for positions/speeds:
      $flags = SwissEph::SEFLG_SPEED | SwissEph::SEFLG_J2000;
      $xx = SwissEph::getFFI()->new("double[6]");
      $result = SwissEph::swe_calc_ut($jd, SwissEph::SE_MARS, $flags, $xx, $serr);
      
  4. House Systems:

    • Calculate houses with swe_house_pos:
      $house = SwissEph::swe_house_pos($jd, SwissEph::SEFLG_SWIEPH, SwissEph::SE_PLACIDUS, $lat, $lon, $xx);
      
  5. Error Handling:

    • Check return codes and $serr buffer:
      if ($result < 0) {
          throw new \RuntimeException(SwissEph::getFFI()->string($serr));
      }
      

Integration Tips

  • Laravel Service Providers: Bind the FFI instance in AppServiceProvider:

    public function register()
    {
        $this->app->singleton(SwissEphFFI::class, function ($app) {
            return new SwissEphFFI($app['config']['swiss-ephemeris.library_path']);
        });
    }
    
  • Caching:

    • Cache JD conversions or ephemeris results in Laravel’s cache:
      $jd = Cache::remember("jd_{$year}_{$month}_{$day}", now()->addHours(1), function () use ($year, $month, $day) {
          return SwissEph::swe_julday($year, $month, $day, 0, SwissEph::SE_GREG_CAL);
      });
      
  • Batch Processing:

    • Use swe_calc_ut in loops for multiple planets:
      $planets = [SwissEph::SE_MERCURY, SwissEph::SE_VENUS, SwissEph::SE_EARTH];
      foreach ($planets as $planet) {
          $xx = SwissEph::getFFI()->new("double[6]");
          SwissEph::swe_calc_ut($jd, $planet, 0, $xx, $serr);
          // Process $xx[0] (longitude), $xx[1] (latitude), etc.
      }
      
  • Asteroids/Comets:

    • Use swe_calc_ut with SE_AST_* constants:
      $asteroid = SwissEph::SE_AST_433; // Eros
      $xx = SwissEph::getFFI()->new("double[6]");
      SwissEph::swe_calc_ut($jd, $asteroid, 0, $xx, $serr);
      

Laravel-Specific Patterns

  1. Publish Ephemeris Files:

    php artisan vendor:publish --provider="SwissEph\FFI\SwissEphFFIServiceProvider" --tag="ephe-files"
    

    Files land in storage/app/swisseph/.

  2. Config Overrides:

    // config/swiss-ephemeris.php
    'library_path' => env('SWISS_EPHEMERIS_LIBRARY_PATH', null),
    'ephe_path' => storage_path('app/swisseph'),
    
  3. Queue Jobs:

    • Offload heavy calculations to queues (FFI is not thread-safe):
      dispatch(new CalculateHoroscopeJob($jd, $lat, $lon));
      

Gotchas and Tips

Pitfalls

  1. Singleton Behavior:

    • Issue: Setting setLibraryPath() after instantiation may fail silently.
    • Fix: Initialize with the correct path:
      $sweph = new SwissEphFFI('/custom/path/to/libswisseph.so');
      
  2. FFI Memory Management:

    • Issue: FFI buffers (double[6], char[256]) must be reallocated for each call or reused carefully.
    • Fix: Reuse buffers in loops or allocate fresh ones:
      $xx = SwissEph::getFFI()->new("double[6]"); // Allocate once and reuse
      
  3. Thread Safety:

    • Issue: FFI is not thread-safe. Concurrent calls from multiple threads/processes may corrupt memory.
    • Fix: Use Laravel queues or ensure single-threaded execution.
  4. Ephemeris File Paths:

    • Issue: Missing .se1 files cause swe_calc_ut to return -1 with error "no ephemeris file".
    • Fix: Publish files or set ephe_path in config:
      SwissEph::setEphePath('/custom/ephe/path');
      
  5. Date/Time Precision:

    • Issue: Floating-point hours (e.g., 12.5 for 12:30 PM) must use . not , in some locales.
    • Fix: Use 12.5 (not 12,5) or enforce setlocale(LC_TIME, 'C').
  6. AGPL-3.0 Compliance:

    • Issue: AGPL requires open-sourcing if using in SaaS or proprietary software.
    • Fix: Review Astrodienst’s license or use a commercial license.

Debugging Tips

  1. Error Codes:
    • Check `swephinfo
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/graphviz
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata