I did write a simple Interface under app\Libs
. I registered the namespace in composer.json
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Libs"
],
DomoticControllerInterface.php (the name "Controller" because this is a controller for domotic setup, not the "Laravel Controller ;)"
namespace App\Libs;
interface DomoticControllerInterface
{
/**
* Get the current temperature.
*/
public function getCurrentTemperature();
/**
* Get the current status of general heating (ON/OFF)
*/
public function getCurrentStatusOfGeneralHeating();
}
And a class Domoticz.php that implements it
<?php
/**
* Domoticz instance.
*
* @see www.domoticz.com
*/
namespace App\Libs;
class Domoticz implements DomoticControllerInterface
{
public function getCurrentTemperature()
{
// TODO: Implement getCurrentTemperature() method.
return "27.4";
}
public function getCurrentStatusOfGeneralHeating()
{
// TODO: Implement getCurrentStatusOfGeneralHeating() method.
}
}
I wrote a HeaterService.php (model? Provider?) under app\Libs
<?php
/**
* Heater Service Class.
* This class perform work on Heater
*
* @since 3.0.0
* @author sineverba
*/
namespace App\Libs;
/**
* Class HeaterService
* @package App\Libs
*/
class HeaterService
{
/**
* The domotic controller.
*
* @var object
*/
private $domotic_controller;
public function __construct($domotic_controller)
{
$this->setDomoticController($domotic_controller);
}
/**
* Get the current temperature
*
* @return string the current temperature
*/
public function getCurrentTemperature()
{
return $this->getDomoticController()->getCurrentTemperature();
}
/**
* Set the domotic controller.
*
* @param object the domotic controller to use
* @since 1.0.0
* @author sineverba
*/
private function setDomoticController($domoticController)
{
$this->domotic_controller = $domoticController;
}
/**
* Get the istance of domotic controller
*
* @return object the domotic controller
* @since 3.0.0
* @author sineverba
*/
private function getDomoticController()
{
return $this->domotic_controller;
}
}
Finally, in my BaseController.php under app\Http\Controllers
<?php
/**
* Base Controller that redirect to right controller/model,
* based on the URL params.
*
* @author sineverba
* @since 1.0.0
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BaseController extends Controller
{
//
/**
* Get the param from the URL and redirect to right controller.
*
*/
public function getTheScriptFromUrl(Request $request)
{
$domotic_controller = env("DOMOTIC_CONTROLLER", "Domoticz");
if ($domotic_controller=="Domoticz") {
$dom_controller = new \App\Libs\Domoticz();
}
$heater = new \App\Libs\HeaterService($dom_controller);
echo $heater->getCurrentTemperature();
}
}
It works. I got the 27.4 hardcoded.
But, is this the right mode to use?
Another one question, I would use the Type-Hinting in HeaterService.php constructor, but don't know how to do.
Finally, my app works (for the moment), without binding. Is it possible?
Thank you very much