K
K
Khaybulla Musaev2015-11-17 17:02:44
symfony
Khaybulla Musaev, 2015-11-17 17:02:44

How to connect a third-party library to Symfony 2?

Hello, dear IT-specialists.
The project needs to include a third-party library, which is essentially just one file with a class. Not a bundle, just a library.
We have Symfony with composer .
After a long googling, I did the following:
1. I tried to fit the class to the PSR-0
standard 2. I added a file with composer.json
3. I uploaded it to github ( https://github.com/musaev-haybulla/FloydWarshall)
4. I uploaded it to packagist ( https://packagist.org/packages/musaevhs/floyd-warshall)
5. Attached it to the project with the composer require "musaevhs/floyd-warshall:dev-master" command .
6. I try to call in the service like this:

use Musaevhs;

class RoutePlanService
{

/**
     * @param RoutePlan $routePlan
     * @return array
     */
    public function minRoute(RoutePlan $routePlan)
    {
        $fl = new \FloydWarshall([0 => [1], [1] => [0]]);
    }
}

What I get:
The autoloader expected class "FloydWarshall" to be defined in file "/Users/musaevhs/srv/tabularium/vendor/musaevhs/floyd-warshall/src/FloydWarshall.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
7. Google, we find instructions in the class to set <?php, but in my class this is set.
While in a stupor, I do not understand where and how I made a mistake.
The connection procedure, of course, is like docking spaceships...
Do you have any idea how I can use the class in my project?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2015-11-17
@musaev_haybulla

The file should be /Musaevhs/FloydWarshall.php
In composer /src/Musaevhs/FloydWarshall.php is equivalent to Musaevhs\FloydWarshall
With your
"psr-0": { "FloydWarshall": "src/" }
would be
use FloydWarshall\Musaevhs \Floyd Warshall; (With folder added)
use FloydWarshall\FloydWarshall; (If you remove the namespace in the file)
use Musaevhs\FloydWarshall; (If you remove FloydWarshall in composer)
use FloydWarshall; (If namespace and FloydWarshall are in composer)
"psr-0": { "": "src/FloydWarshall" } - (In theory it won't work)

В общем измените структуру
src/
    Musaevhs/ <-- добавив папку
        FloydWarshall.php
composer
"psr-0": { "": "src/" }
И подключение либы
use Musaevhs\FloydWarshall;

www.php-fig.org/psr/psr-0/ru Deprecated
See www.php-fig.org/psr/psr-4/ru

J
jaxel, 2015-11-17
@jaxel

And what for such complexities for the sake of one file with a class? I see no reason to upload someone else's library to packagist. Just put it in your bundle, assign namespaces to it, make a service if necessary, and that's it.

M
Max, 2015-11-17
@AloneCoder

$fl = new Musaevhs\FloydWarshall([0 => [1], [1] => [0]]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question