views:

53

answers:

1

Hi,

I am developing an api layer for my application. I have designed a structure and need some advice/feedback for it. You can find the basic implementation of the structure at the bottom.

Here are my requirements for the structure:

  • Response from API commands may need to be formatted in different formats (JSON,XML,etc.)
  • Some API commands may require authentication, some may not
  • Every API command should be open to extension via plugins (Notification on events, filtering of input/output paramters, etc.)

With these requirements in mind I have applied Decorator pattern to my API layer. I am not sure if I have designed the structure right and need to be sure about it.

The last item in requirements list is not covered in the implementation below because I am still trying to figure out how to do that.

What do you think? Am I on the right path?

<?php

// Interfaces
interface I_API_Command {}

// Abstract classes
abstract class A_API_Command implements I_API_Command
{
    abstract public function run();
}

abstract class A_Decorator_API_Command implements I_API_Command
{
    protected $_apiCommand;
    public function __construct(I_API_Command $apiCommand) {
        $this->_apiCommand = $apiCommand;
    }
    abstract public function run();
}

// Api command class
class APIC_Tasks_Get extends A_API_Command
{
    public function run() {
        // Returns tasks
    }
}

// Api command decorator classes
class APICD_Auth extends A_Decorator_API_Command
{
    public function run() {
        // Check authentication
        // If not authenticated: return error

        // If authenticated:
        return $this->_apiCommand->run()
    }
}

class APICD_JSON_Formatter extends A_Decorator_API_Command
{
    public function run() {
        return json_encode($this->_apiCommand->run());
    }
}

// Usage
$apiCommand = new APICD_JSON_Formatter(new APICD_Auth(new APIC_Tasks_Get()));
$apiCommand->run();

?>
A: 

In my opinion... i think that old-classic MVC would be enough..

Response from API commands may need to be formatted in different formats (JSON,XML,etc.)

The controller would read the request and change the view to output the information in the selected format. Or you could pass the request to the View and the view would change the output format.

Some API commands may require authentication, some may not

That's also a task for the controller, to read and validate the request. If the user is not authenticated it would change the response

Every API command should be open to extension via plugins (Notification on events, filtering of input/output paramters, etc.)

Now this is the tricky part.. you could modify the controller and implement the strategy pattern. That's a good idea if you're planning to constantly change your plugins.

In my case, i have multiple controllers and i use a Controller Factory that reads the request and returns a controller that manages that request.

I am not really sure how do you want to implement your plugins. When you say Notification on events it sounds to me that you could use the observer pattern, and filtering of input/output paramters seems like a controller's task.

I hope this helps. Good Luck

pleasedontbelong
@pleasedontbelong Please correct me if I am wrong. What you suggest is that for each API command, I need a controller. For example; for API command class APIC_Tasks_Get, I need a controller to call that command , check auth if required and return the correct view depending on the request type?
matte
mmm you could have a controller for each command.. but you might end up with a lot of controllers.. the idea is to group your api commands on a few controllers. In your exemple: I'd have a class APIC_Task_Controller that manages APIC_Tasks_Get, APIC_Tasks_Set, APIC_Tasks_Describe, etc.
pleasedontbelong
nice idea, thanks!
matte