Uncategorized

The definitive introduction to D8 and Composer

Note: This blog post was written in 2015 to cover Drupal 8.0, and is no longer up to date. Composer Manager is deprecated and should not be used. Here’s an updated version.

We’re making a new site based on Drupal 8. This site needs to display addresses, and some quick research leads us to the Address module.
However, after downloading and trying to enable Address, we get the following message: Address requires the commerceguys/addressing library.

In the old days a requirement like this would be satisfied by manually downloading and extracting the library into sites/all/libraries/addressing. Nowadays that’s no longer possible, PHP libraries must be installed using Composer.

What’s Composer?

Composer is a tool for managing dependencies on the project level, a project being your site or web application.

Let’s say our (non-Drupal) site needs the commerceguys/intl and commerceguys/addressing libraries (“packages” in Composer-speak). We would create a composer.json file, and list the dependencies there:

1
2
3
4
5
6
{
    "require": {
        "commerceguys/intl": "dev-master",
        "commerceguys/addressing": "dev-master"
    }
}

Available packages are listed on Packagist, a central repository anyone can add to. Running composer install will download the dependencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing commerceguys/intl (dev-master d4dca7e)
    Cloning d4dca7e4d8e5d5cee65c0e987e650daa056f3948
  - Installing commerceguys/enum (v1.0)
    Loading from cache
  - Installing doctrine/collections (v1.3.0)
    Loading from cache
  - Installing commerceguys/addressing (dev-master 98e2cae)
    Cloning 98e2caedbd71a838a7b2ececbe7691d480b0f6a5
Writing lock file
Generating autoload file

Notice how Composer has installed two more packages we didn’t ask for.
Those are the dependencies of our dependencies. You can see them listed in the commerceguys/addressing composer.json file.

Once installed, packages can be kept up to date by running composer update.
Composer knows which versions it’s allowed to install/update to by looking at the composer.json file. The dev-master in our examples means that it should always fetch the latest dev release. We could instead use wildcards to allow only releases of a specific branch.

If you’ve ever used Drush Make to download Drupal modules and themes, then all of this sounds familiar. You can think of Composer as the more advanced Drush Make that works for all PHP projects and packages. Compared to Drush Make, Composer has the benefit of being able to recursively resolve dependencies (downloading the dependencies of each dependency) and being able to detect conflicts.
For example, let’s say package A and package B both require Guzzle, the HTTP client. However, package A specifies that it only works with Guzzle5, while package B specifies that it only works with Guzzle6. In this case Composer will abort the install process and warn us.

Composer is clearly an awesome tool, but why do we NEED to use it? Using Drush Make was always optional for site builders. The secret lies in the last line of Composer output: Generating autoload files.
Modern applications such as Drupal 8 consist of many classes, so it would be impractical and costly to manually include each one. Instead, the application includes one special class, called the autoloader which then automatically includes other classes when they are first needed. When Composer runs, it regenerates the autoloader, giving it the locations of the newly downloaded dependencies.

 

View More 501
View More 502
View More 503
View More 504
View More 505
View More 506
View More 507
View More 508
View More 509
View More 510
View More 511
View More 512
View More 513
View More 514
View More 515
View More 516
View More 517
View More 518
View More 519
View More 520
View More 521
View More 522
View More 523
View More 524
View More 525
View More 526
View More 527
View More 528
View More 529
View More 530
View More 531
View More 532
View More 533
View More 534
View More 535
View More 536
View More 537
View More 538
View More 539
View More 540
View More 541
View More 542
View More 543
View More 544
View More 545
View More 546
View More 547
View More 548
View More 549
View More 550
View More 551
View More 552
View More 553
View More 554
View More 555
View More 556
View More 557
View More 558
View More 559
View More 560
View More 561
View More 562
View More 563
View More 564
View More 565
View More 566
View More 567
View More 568
View More 569
View More 570
View More 571
View More 572
View More 573
View More 574
View More 575
View More 576
View More 577
View More 578
View More 579
View More 580
View More 581
View More 582
View More 583
View More 584
View More 585
View More 586
View More 587
View More 588
View More 589
View More 590
View More 591
View More 592
View More 593
View More 594
View More 595
View More 596
View More 597
View More 598
View More 599
View More 600
View More 601
View More 602
View More 603
View More 604
View More 605
View More 606
View More 607
View More 608
View More 609
View More 610
View More 611
View More 612
View More 613
View More 614
View More 615
View More 616
View More 617
View More 618
View More 619
View More 620
View More 621
View More 622
View More 623
View More 624
View More 625
View More 626
View More 627
View More 628
View More 629
View More 630
View More 631
View More 632
View More 633
View More 634
View More 635
View More 636
View More 637
View More 638
View More 639
View More 640
View More 641
View More 642
View More 643
View More 644
View More 645
View More 646
View More 647
View More 648
View More 649
View More 650
View More 651
View More 652
View More 653
View More 654
View More 655
View More 656
View More 657
View More 658
View More 659
View More 660
View More 661
View More 662
View More 663
View More 664
View More 665
View More 666
View More 667
View More 668
View More 669
View More 670
View More 671
View More 672
View More 673
View More 674
View More 675
View More 676
View More 677
View More 678
View More 679
View More 680
View More 681
View More 682
View More 683
View More 684
View More 685
View More 686
View More 687
View More 688
View More 689
View More 690
View More 691
View More 692
View More 693
View More 694
View More 695
View More 696
View More 697
View More 698
View More 699
View More 700
View More 701
View More 702
View More 703
View More 704
View More 705
View More 706
View More 707
View More 708
View More 709
View More 710
View More 711
View More 712
View More 713
View More 714
View More 715
View More 716
View More 717
View More 718
View More 719
View More 720
View More 721
View More 722
View More 723
View More 724
View More 725
View More 726
View More 727
View More 728
View More 729
View More 730
View More 731
View More 732
View More 733
View More 734
View More 735
View More 736
View More 737
View More 738
View More 739
View More 740
View More 741
View More 742
View More 743
View More 744
View More 745
View More 746
View More 747
View More 748
View More 749
View More 750
View More 751
View More 752
View More 753
View More 754
View More 755
View More 756
View More 757
View More 758
View More 759
View More 760
View More 761
View More 762
View More 763
View More 764
View More 765
View More 766
View More 767
View More 768
View More 769
View More 770
View More 771
View More 772
View More 773
View More 774
View More 775
View More 776
View More 777
View More 778
View More 779
View More 780
View More 781
View More 782
View More 783
View More 784
View More 785
View More 786
View More 787
View More 788
View More 789
View More 790
View More 791
View More 792
View More 793
View More 794
View More 795
View More 796
View More 797
View More 798
View More 799
View More 800
View More 801
View More 802
View More 803
View More 804
View More 805
View More 806
View More 807
View More 808
View More 809
View More 810
View More 811
View More 812
View More 813
View More 814
View More 815
View More 816
View More 817
View More 818
View More 819
View More 820
View More 821
View More 822
View More 823
View More 824
View More 825
View More 826
View More 827
View More 828
View More 829
View More 830
View More 831
View More 832
View More 833
View More 834
View More 835
View More 836
View More 837
View More 838
View More 839
View More 840
View More 841
View More 842
View More 843
View More 844
View More 845
View More 846
View More 847
View More 848
View More 849
View More 850
View More 851
View More 852
View More 853
View More 854
View More 855
View More 856
View More 857
View More 858
View More 859
View More 860
View More 861
View More 862
View More 863
View More 864
View More 865
View More 866
View More 867
View More 868
View More 869
View More 870
View More 871
View More 872
View More 873
View More 874
View More 875
View More 876
View More 877
View More 878
View More 879
View More 880
View More 881
View More 882
View More 883
View More 884
View More 885
View More 886
View More 887
View More 888
View More 889
View More 890
View More 891
View More 892
View More 893
View More 894
View More 895
View More 896
View More 897
View More 898
View More 899
View More 900
View More 901
View More 902
View More 903
View More 904
View More 905
View More 906
View More 907
View More 908
View More 909
View More 910
View More 911
View More 912
View More 913
View More 914
View More 915
View More 916
View More 917
View More 918
View More 919
View More 920
View More 921
View More 922
View More 923
View More 924
View More 925
View More 926
View More 927
View More 928
View More 929
View More 930
View More 931
View More 932
View More 933
View More 934
View More 935
View More 936
View More 937
View More 938
View More 939
View More 940
View More 941
View More 942
View More 943
View More 944
View More 945
View More 946
View More 947
View More 948
View More 949
View More 950
View More 951
View More 952
View More 953
View More 954
View More 955
View More 956
View More 957
View More 958
View More 959
View More 960
View More 961
View More 962
View More 963
View More 964
View More 965
View More 966
View More 967
View More 968
View More 969
View More 970
View More 971
View More 972
View More 973
View More 974
View More 975
View More 976
View More 977
View More 978
View More 979
View More 980
View More 981
View More 982
View More 983
View More 984
View More 985
View More 986
View More 987
View More 988
View More 989
View More 990
View More 991
View More 992
View More 993
View More 994
View More 995
View More 996
View More 997
View More 998
View More 999
View More 1000

Related Articles

Leave a Reply

Back to top button