php-deal / framework

Design by Contract framework for PHP
MIT License
252 stars 22 forks source link

Setup with symfony and better doc #34

Closed lcetinsoy closed 4 years ago

lcetinsoy commented 4 years ago

Hello !

I am trying to use this framework which I always found very interesting but a bit difficult to setup. It is not the first time I fail to set it up with Symfony. I was not able to make a simple contract fails. Then I tried to use the demo to see if it was working on my setup and it is a bit tedious to setup (but I succeed ! ).

I think that this project is very nice and that Design By Contract should have better recognition. I think that with a slightly better documentation your tool could be much easier to use and has more user. Its pities me that its potential remains untapped.

So my issue has two points :

  1. Can you help me for setting it up with symfony ? [edit : think I got it]
  2. Ease demo testing (very easy)

I am willing to do a PR with docs and example when I solve my use case (even doing a blog post if time someday).

  1. Symfony (3.4) setup

I tried to the following code in my app_dev.php file with an obvious contract fail and everything goes normally, even if I do put it in my app_dev.php. Any Hint ?

Contract in some subfolder of "src" folder :

namespace Blabla\Model;

use PhpDeal\Annotation as Contract;

/* 
 * @Contract\Invariant("$this->name > 1000")
 */
class MyClass {

    public $name;

    public function setName() {
        return $this->name = "coucou";
    }
}

in my app_dev.php :

<?php

ini_set('memory_limit', '512M');

use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
use PhpDeal\ContractApplication;

require __DIR__.'/../vendor/autoload.php';

$instance = ContractApplication::getInstance();
$instance->init(array(
    'debug'    => true,
    'appDir'   => __DIR__,
    'cacheDir' => __DIR__.'/../var/cache/phpdeal/',
    'includePaths' => array(
            __DIR__ . '/../src/'
        )
));

$backend = new Backend\Model\Backend();
$backend->setName(); // !! should fail !!

$kernel = new AppKernel('dev', true);
if (PHP_VERSION_ID < 70000) {
//    $kernel->loadClassCache();
}
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
  1. Easing Demo and docs

Regarding the demo it could be nice to move the files in demo into a sub folder "src" and ad a composer json in the demo folder with the following values. So you just clone cd demo && composer install && php demo.php and works in one line :)

{
    "name": "phpdeal/demo",
    "authors": [
        {
            "name": "bla",
            "email": "bla"
        }
    ],
    "require": {
        "php-deal/framework": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "Demo\\":"src/Demo"
        }
    }
}

Thanks in advance !

[edit] : showing the list of parsed classes and available contracts could be nice, looking in code now. [edit2] : think symfony is ok, needed to change app path to DIR '/../' even though I had an explicit includePaths, can you explain why ?

icanhazstring commented 4 years ago

Hi thanks for reaching out! This lib hasn't had any updates lately, but should get some love 😉

As for the demo repository that is a great idea. I'll take you example and will se to set it up.

For the symfony part: I am not very familiar with symfony, but will try to see why you needed to change the app path.

Also I would appreciate a PR with a doc update on how to use it with symfony 👍