Skip to content

Commit

Permalink
Attempt 1 at creating a composer package, putting it on Packagist, an…
Browse files Browse the repository at this point in the history
…d including it in my project.
  • Loading branch information
Steven Barnett committed Feb 24, 2015
1 parent 1757499 commit 29835f8
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "stevendesu/world-distance",
"description": "A Laravel 5 package for calculating the distance between two given points on a globe. Optionally uses Google's Distance Matrix API. By default just uses math and assumes a spherical Earth.",
"keywords": ["laravel", "laravel 5", "distance", "matrix", "geography"],
"license": "MIT",
"authors": [
{
"name": "Steven Barnett",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "~5.0"
},
"autoload": {
"psr-4": {
"Stevendesu\\WorldDistance\\": "src"
}
},
"minimum-stability": "stable"
}
18 changes: 18 additions & 0 deletions config/worlddistance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return array(

/*
|--------------------------------------------------------------------------
| Earth radius
|--------------------------------------------------------------------------
|
| This is the value that will be used in distance calculations. The unit on
| this value equals the unit that will be returned. e.g. if this value is
| in meters, the return value will be in meters.
|
*/

'earth_radius' => 6378100, // Meters

);
56 changes: 56 additions & 0 deletions src/WorldDistance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php namespace Stevendesu\WorldDistance;

// For Google Maps API caching
use Stevendesu\WorldDistance\WorldDistanceRecord;

// For logging major fail
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

// Default like units, base location, or Earth size
use Illuminate\Config\Repository;
use Illuminate\Database\DatabaseManager;

class WorldDistance {

/**
* Illuminate config repository instance.
*
* @var \Illuminate\Config\Repository
*/
protected $config;

/**
* Illuminate database manager instance.
*
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;

/**
* Create a new WorldDistance instance.
*
* @param \Illuminate\Config\Repository $config
* @param \Illuminate\Database\DatabaseManager $database
*/
public function __construct(Repository $config, DatabaseManager $database) {
$this->config = $config;
$this->database = $database;
}

// calculate the distance in kilometers
public function getDistance($LatLng1, $LatLng2, $googleMaps = false) {
if( $googleMaps ) {
return $this->getGoogleMapsDistance($LatLng1, $LatLng2);
}

$dLat = deg2rad($LatLng2[0] - $LatLng1[0]);
$dLon = deg2rad($LatLng2[1] - $LatLng1[1]);

$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($LatLng1[0])) * cos(deg2rad($LatLng2[0])) * sin($dLon/2) * sin($dLon/2);
$b = 2 * asin(sqrt($a));
$c = $this->config->get('worlddistance.radius') * $b;

return $c;
}
}
14 changes: 14 additions & 0 deletions src/WorldDistanceFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace Stevendesu\WorldDistance;

use Illuminate\Support\Facades\Facade;

class WorldDistanceFacade extends Facade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'distance'; }

}
52 changes: 52 additions & 0 deletions src/WorldDistanceServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php namespace Stevendesu\WorldDistance;

use Illuminate\Support\ServiceProvider;

class WorldDistanceServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('stevendesu/world-distance');

$this->publishes([
__DIR__.'/../config/worlddistance.php' => config_path('worlddistance.php'),
]);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// Register providers.
$this->app['distance'] = $this->app->share(function($app)
{
return new WorldDistance($app['config'], $app['db']);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['distance'];
}

}

0 comments on commit 29835f8

Please sign in to comment.