dongzhang6544 2019-08-16 03:48
浏览 100
已采纳

“作曲家”包经理如何运作?

I am trying to install "Krumo"

It says there are two ways to install, I tried the first one (download the PHP file and include it into my project) and it worked fine.

Now I am trying the second way (using composer).

enter image description here

Bunch of questions emerge at the second I see it.

  • Where to run this command?
  • Is it equivalent to downloading the "class.krumo.php" file and other skin files to the current folder?
  • Do I still need to include the file in my PHP?
  • Or, maybe through running this command, krumo becomes a built-in function of PHP on my machine (so I can use it "out-of-box" on any PHP file)?

I managed to find that this install command doesn't actually work (probably outdated), and found out that I had to run composer require kktsvetkov/krumo. I did so and got this:

composer output

It seems to me it is finally installed. Under the folder there are only two files added "composer.lock" and "composer.json", the class.krumo.php file is nowhere to be found, and of course calling krumo() in a test PHP file throws the error call to undefined function krumo.

I need a big picture of how composer packages work.

  • 写回答

1条回答 默认 最新

  • douxiajiao8445 2019-08-17 12:23
    关注

    First, you need to understand what composer is. It's a "dependency manager". So it manages your application dependencies, basically the libraries your application needs to work.

    It does so recursively. So if your application requires NiceDependency to work, and NiceDependency in turn requires AnotherNicePackage, it installs both. It deals also with conflict resolution (when one of your dependencies requires something that's not compatible with something that another of your dependencies require).

    The file where your dependencies are declared is composer.json.

    So when you run composer require [some-vendor/some-package], a few things happen behind the curtain. Simplifying things a lot:

    • If your composer.json file doesn't exist, it will create it.
    • It will try to find your dependency in the central repository (packagist.org)
    • If found, it will download the package and store it in the vendor directory.
    • It will update your composer.json it to add your dependency to the require key.

    In the process, it will resolve all the nested dependencies and do the same for those.

    When it's done, it will also create a composer.lock file.

    This "lock" file stores a frozen snapshot of all the references to all the packages that were actually installed. This is necessary because when you declare your dependencies you can define a range of versions (e.g "anything greater or equal than version 2.2; but lower than version 2.3"). Your composer.lock would store the specific version that's actuall installed (e.g. "version 2.2.4").

    Later, if someone got your project files and executed composer install, the lock file would be read so they installed exactly the same files as you did.

    (require adds a dependency to your project's composer.json file; install reads your composer.json and composer.lock files and sets up a project from there; there is also a update command that would read only composer.json, download the latest available packages respecting to the version restrictions in each dependency, and update `composer.lock accordingly)

    Additionally, composer helps with autoloading, to make the process of actually using the installed libraries easier and faster for developers.

    Autoloading is very convenient. Not only you no longer have to add a require someclass.php; statement for each class you want to use, but you also gain the advantage of not having to read these files until they are actually needed.

    So not only it simplifies using these new classes, it helps making your application perform better.

    For this, inside the vendor directory a file named autoload.php is created. Typically, you need to require this file as the first thing you do on your application entry point.

    For example, assuming you have a structure like this:

    - project root/
    --- composer.json
    --- composer.lock
    --- vendor/
    --- public/
    ----- index.php
    

    Your index.php file should read:

    // public/index.php
    <?php
    require('../vendor/autoload.php');
    
    

    This would allow you to use any installed library normally. In the case of the tool you want to install:

    // public/index.php
    <?php
    require('../vendor/autoload.php');
    
    $a = [
        'foo' => 'bar',
        'baz' => [1, 2, 3],
        'xxx' = false
    ];
    
    krumo($a);
    
    

    As a side note, that library seems to be quite old. I'd try to get something a bit newer. I'd recommend Symfony's VarDump component.

    And no, it is not a particularly friendly "newbie" tool. It helps dealing with a lot of things, but it's mostly aimed to slightly more advanced users, since it helps solving issues that aren't so significant in starter/very simple projects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vc6.0中想运行代码的时候总是提示无法打开文件是怎么回事
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题