Category Archives: Composer

PHP + Composer: The Process “php -S 0.0.0.0:8080 -t public index.php” Exceeded the Timeout of 300 Seconds

PROBLEM

Given the following composer.json

{
  ...
  "scripts": {
    "start": "php -S 0.0.0.0:8080 -t public index.php"
  }
}

When running composer start

The PHP built-in web server stops with the following error message…

[Symfony\Component\Process\Exception\ProcessTimedOutException]                              
The process "php -S 0.0.0.0:8080 -t public index.php" exceeded the timeout of 300 seconds.  

SOLUTION

There are several ways to extend the timeout value, but here is one way to do it through composer.json.

To extend the timeout value from 300 seconds to 2000 seconds, add the following config block in composer.json:-

{
  ...
  "scripts": {
    "start": "php -S 0.0.0.0:8080 -t public index.php"
  }
  "config": {
    "process-timeout": 2000
  }
}

To disable web server timeout completely, set process-timeout to 0:-

{
  ...
  "scripts": {
    "start": "php -S 0.0.0.0:8080 -t public index.php"
  }
  "config": {
    "process-timeout": 0
  }
}
Advertisements

Slim: Class ‘X’ Not Found

PROBLEM

Let’s assume we have the following project structure with src/model/Person.php created…

.
├── composer.json
├── composer.lock
├── logs
├── public
├── src
│   ├── model
│   │   └── Person.php
│   ├── dependencies.php
│   ├── middleware.php
│   ├── routes.php
│   └── settings.php
├── templates
├── tests
└── vendor

… and the class may look something like this…

// src/main/Person.php

class Person
{
    public $name;
    public $age;

    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
}

… and it is being used in other file…

// src/routes.php

use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
    $person = new Person('mike', 50);

    // ... do something
    return ...
});

When hitting the given route from the browser, we get the following error:-

Type   : Error
Message: Class 'Person' not found
File   : /path/to/app/src/routes.php
Line   : 7

SOLUTION #1: Using import/require

One way to solve this is to use include or require statement:-

// src/routes.php

require 'model/Person.php';

use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
    $person = new Person('mike', 50);

    // ... do something
    return ...
});

SOLUTION #2: Using autoloading

Another approach is to leverage PSR-4 autoloading.

First, create a namespace in Person.php:-

// src/main/Person.php

namespace App\Model;

class Person
{
    public $name;
    public $age;

    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
}

Second, in the file that consumes that model, specify a use statement to alias that model so it doesn’t need to be fully qualified with the given namespace in order to use it:-

// src/routes.php

use App\Model\Person;
use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
    $person = new Person('mike', 50);

    // ... do something
    return ...
});

Third, in composer.json, create the autoload block and specify the namespace mapping in it:-

{
  ...
  "autoload": {
    "psr-4": {
      "App\\Model\\": "src/model/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\\": "tests/"
    }
  },
  ...
}

Finally, run composer dump-autoload to regenerate vendor/composer/autoload_psr4.php. This file keeps track the list of classes needed to be included in the project.