Dieses Blog durchsuchen

Sonntag, 16. Oktober 2016

php: calling protected methods without inheritance

Sometimesyou need to call a protected method without inherit from it.
This can be done by integrating the magic method __call()

Here is an example from php.net



class Fun
{
protected function debug($message)
{
echo "DEBUG: $message\n";
}

public function yield_something($callback)
{
return $callback("Soemthing!!");
}

public function having_fun()
{
$self =& $this;
return $this->yield_something(function($data) use (&$self)
{
$self->debug("Doing stuff to the data");
// do something with $data
$self->debug("Finished doing stuff with the data.");
});
}

// Ah-Ha!
public function __call($method, $args = array())
{
if(is_callable(array($this, $method)))
return call_user_func_array(array($this, $method), $args);
}
}

$fun = new Fun();
echo $fun->having_fun();

?>

Samstag, 15. Oktober 2016

laravel 5: create professional api with automatic api-docs and rest clients with swagger

Prerequisits

We need some things installed on the local machine.

An Api is nothing without a good documention. Without Api-docs every consumer stands infront of a black box. Its is very hard to discover the api methods and to find out how all works. Even if the api is well coded an the developers know hoow to use it. I will always need support and training to give your api consumers / partners an idea how to manage your api.

This is where swagger and swagger-ui comes into the game.

Swagger is an defacto industry standard to unifiy Rest Apis over the internet. Swagger defines Standard metadatina for  each of your Api methods in JSON format.
No worry, you dont have to write JSON to get it on. You will "only" have to annotate your Api Classes and its methods.

Swagger UI generates a very useful UI for your REST-API by parsing the swagger.json definition


As you can see, this is a User API with 2 Methods in it. 1 User/list and 1 User/get method.

Swagger UI takes the swagger.json output and turns it inti this nice HTML Pages.

On the top of each Item you can display more information or even a REST Client like that:

Generate REST Clients out of the box



This is really awesome.

Add swagger and swagger-ui to your project

Now lets bring it together with laravel

At first you need l5-swagger. This is a cli extension for laravel

Add "darkaonline/l5-swagger": "~3.0 to your composer.json and make "composer update"

Add module creator to your cli

add "artem-schander/l5-modular": "^1.3" to your composer.json and make "composer update"

At first you need l5-swagger. This is a cli extension for laravel

Add "darkaonline/l5-swagger": "~3.0 to your composer.json and make "composer update"

Generate a module

Switch to your laravel project root

php artisan make:module Api 
This will create a Module Api under "app/modules"

Add annotation to your controller

Swagger annotations will describe your Api in a json format. So that it can be used to create a nice webapplication for your Api methods including a complete documentation.

Add a controller like that

As you can see you a lot of annotation there. Do not feel overvelmed from that. It is definetly worth a try. The benefit is a really smooth webui for your documentation within a REST Client for each method. This will definetly be a sales pitch for your Apis.

If you have set the routing to reach you api methods, you can generate the swagger.json by calling the artisan cli like that
php artisan l5-swagger:generate

This will generate a file "<approot>/storage/api-docs/api-docs.json"

Point your browser to that file and copy the url


Start swagger-ui

Open your browser and open a file "<swagger-ui/dist/index.htm>" from the filemenu in your browser

At that you will get the swagger ui.
Now you can paste the swagger.json url to the searchfield an hit "explore"


Now you will see your api documentation:

Thats awesome


apache: enable cors in htaccess

In some cases your javascripts want work if you try to make xhttp requests via ajax.

The browsers stuck on the CORS Policy wich rejects requests across different domains, to prevent xss.

In some cases you want to allow such requests. For instance in a trusted SOA enviroment, where you have to request accross different domains or subdomains.

If you are using apache as your webserver, you can  modify your headers and allow your browser to request via script from a other domain.

Here is how it works.

Add a .htaccess  for your directory where you want to request to:

Enable headers in apache with:
a2enmod headers

You can restrict the request methods by removing it from the list with allowed verbs to stricten security.





git: add / delete / modify a file on your last commit with --amend. Manipulate your history

Sometimes you forget an important change on your last commit and you have to commit a further change to your repo. This will look ugly in your commit history.

Here is a trick to let the forgotten change look like it was added in your last commit. This prevents discussions in your team about the last commits.


git add <forgotten filename>
git commit --amend --no-edit 

If you make a:

git log --graph --all --stat --decorate
It will show up only your last commit with both changes in it. The prevois and the last commit.

Please do only use that on your local history. History manipulation on remote repositories breaks codebases and causes trouble in your dev-team

git: protect you from history overrides and history manipulation

Sometimes you will get in the situation, that your changes simply disapear, even if you are 100% sure, that your have pushed your changes and merged them into master. In the history you cant find your commits.

Well, it seems, that someone has tampered the history with a rebase.

Thats really frustrating, because its hard to proof, and at the end you are looking dump.

Here is a way to protect you from tampering the history or using none fast forwards or deleting history

Edit your ~/.gitconfig


git: let git learn how to solve conflicts automaticly: rerere

Once you have solved a conflict, you can git tell to learn from you.
Activate rerere and next time the same conflicts occures git will solve this conflicts automaticly

git config --global rerere.enabled true

git: add a commited file to .gitignore

Everyones, how anoying it is to have files in the remote repository, which has to be on the .gitignorelist.

Git simply ignores every .gitignore entry, wich was committed already.

Here is a simple trick wich helps you out

git update-index --assume-unchanged <filename>