 Hello everybody, today I'm going to be talking about the PHP library for that, about using reasonable code in your Drupal projects. You can find me on Twitter at JoshTaylor, Drupal.org, my username is JoshTaylor and JoshTow on IRC. So basically with code sharing, why should you reinvent the wheel? There's so many great projects out there for PHP, especially in things like GitHub and other repositories like that. You can do a search and find pretty much anything. The nice thing about Drupal 8 is that they're starting to use that as well. So yeah, it's great. So basically how has the PHP community solved this at large? There's a really good tool that's come out a couple of years ago called Composer. Composer is a tool for dependency management in PHP. So basically you can create a composer.json file which will tell the PHP project about all of your dependencies that you have in PHP for your project. And then you basically just run a command called Composer install and what that will do is it will install all of your dependencies for you. And all of your libraries and dependencies are pulled in automatically from GitHub and other sources. So let's break this down. Basically to install Composer, you can do a command which is basically just curl s and then grab the installer and then pop through to PHP and that will install it for you. And you can use that on your Linux shared server. And then to run it, you just run composer.far and then it'll give you a big output with all the different options that you can use. So basically the Composer.json that lives in the root directory of your project is basically a JSON markup file with all the information about your project and all of your dependencies. Basically this is just what the file looks like. So at the very top you have your name for the project. So basically that's your name space. So mine's called Josh Taylor than some project. Then you have your description, which is some project. And then the keywords that you want to use, your license, which is very important because obviously different licenses have got different restrictions that you can use and then the author information. The reason why you have this file is because there's a service called packages, which basically passes this information, which people can then look up really easily for your project. The next line you can do is basically you can start requiring all the different packages that you need for your project. So for example, I'm using Stripe here, which is Stripe and then Stripe PHP. And then you can define a version. I haven't put a version in, but you can use like Dev Master to grab the latest branch or you can define it by a version name, for example, 1.0, 2.0, stuff like that. So if something breaks in a version upgrade from 1.0 to 2.0, you will have any problems when it comes to that because you get locked to that version. And the next step is to run Composer and Still after creating this file. And this is what it looks like. So you just run Composer and Still. It goes off to GitHub and it says, hey, can you grab me all these files? And then, no, it's a recorded video. I don't know why it's not, oh yeah, guys. So basically, thank God, yes, put up. And then basically it goes and downloads Stripe PHP from the 2.11 version. And if you've already unloaded it, it loads it from Cache. So we're probably having to download the same file over and over again for your project, you can just load it from your local Cache. So it speeds up things by a great amount. And the next thing is how has the Drupal community solved this? So Composer came out after Drupal 7 came out. So people were trying to figure out how to solve library management in Drupal 7. And there's a couple of different ways that they tried to do this. The first version is a module called libraries. Libraries is a Drupal API for referencing libraries. And you can download the library manually and put it into sites or libraries. When you install libraries, you can basically use it as an API. So for example, in the earlier example, you strike for this one here, music drop again. So basically in your install hook, you basically set up where the file exists. So you can't actually install your module until you put the code into your directory. So it's really confusing for people who don't know what they're doing. So basically, and then you have your hook library's info as well inside your module. So for example, you have basically have to list out every single version that you're using, the Stripe, Fender, URL, the download, URL, all that sort of things. So it's like a massive amount of work to actually get this to work. And then step three is access. So when you finish this off for your Drupal 7 project, how do you manage this? Do you commit your library's folder? Do you pull it onto staging or production? How do you actually do this? When you want to upgrade your library, you have to download the library easier manually and then extract it into your sites folder. And that's really tedious. Another solution that came out for Drupal 7 is called Composer Manager. Basically that you use this Composer and you use this Composer.json files in your modules. And it allows you to download Composer libraries through Composer rather than libraries. So basically, it's the same as before where inside of your module file, you create a Composer.json file and you basically do your require and then whatever libraries you require. It's about four lines of code usually to include a library, so it's pretty simple. And then after you've done that, you can basically enable your module. So it's like Drush and then some project and then Composer Manager will enable all the projects and dependencies for that project. So for example, I'm enabling the demo Composer project, sorry module that I've created. And what that will do is because I've got a Composer.json, it's a lot module. It'll start to download all those files that I've defined in that file. Yep, so as you can see, it grabs Stripe PHP again from the cache. So that took about 10 seconds to grab. And from now on, rather than having to do the library's hook, you can just basically define a version number inside of your Composer.json, which is great. And so through success. So now you can use Composer.json. It makes developers live as much easier because now you don't have to commit all the vendor files. You can basically just tell your developers to do a Drush Composer install and it'll grab all the files for that developer and you can sort of do it on staging and production as well quite easily. The digital plate solution for this right now is Composer Manager. Each module has got a Composer.json same as before. The major difference between this and this Drupal 7 version is that if you have multiple modules inside of your Drupal 8 project, it basically searches for each of those folders recursively and then downloads them. So if you have dead projects sitting around, like I'm sure everyone does, it'll actually search through those Composer.json files as well, which is really confusing. So just make sure that you delete any modules that you're not using in your Drupal 8 project. So it's the same as before. So you basically have a Composer.json in your module. So you have require, Stripe, PHP and the version number. And then step two is to enable Composer Manager. So basically you do Drush and Composer Manager and Composer Manager install. Then after that you can just run Drush Composer Manager in it. What that does is it generates a Composer.json in the core directory of Drupal 8 and then which you can then go and overwrite as you need. So basically that will go and search for all of the Composer.json files in all your modules and create a new Composer.json for that. Then you need a CD into the core directory and run Composer.jsonUpdate. And what that will do is it's basically the exact same command as Composer.update, but it will rebuild your Composer.json and download the new libraries for you. And then after that you can start installing your modules and stuff as usual and it will do it for the same as Drupal 7 version. So don't move for this. So I'm basically doing a Drush Composer Manager in it, which basically initializes Composer Manager and then I'm seeding it into core and then I'll do a Composer Drupal rebuild. And what that does is it creates the new Composer.json using all my libraries that I've defined in my modules file and then I'll do a Composer.update which basically goes and updates all the modules. But because I've already done this, it basically didn't know anything. So there's a couple of good community resources for about Composer. There's group.drupal.org.composer, which is a user group for Composer. It's quite active. There's quite a few threads there. And the drupal.org.project.composer.manager, which is basically the Composer Manager for 7 and 8, which you can go and download. So basically the next thing is how to actually create libraries for Composer. The second one is to create your new awesome library, as you normally do. You create a Composer.json file. You submit to Packagist. Packagist is the PHP repository for hosting all of the repos. And then you can start using that new code in all your projects. So what Packagist is, is basically package.org. You can go to it. You can find great new repositories. You can submit your own. You can do all that sort of stuff. So to find a library, you can type in drupal, for example, and it comes up with drupal-drupal, which is the drupal8 code. And it has the drupal extension, drupaldriver, drupalcoder, like all libraries which you can start using with drupal. There's also a new initiative to create a Packagist just for drupal, because there's going to be thousands and thousands of modules. And people don't want to overwhelm the Packagist website. So basically rather than offloading it to a third party, drupal can handle all themselves. Since Packagist is open source, they can pretty much just do a clone of it themselves and then create Packagist that way. Yes, correct. Yes, correct. So basically drupal Packagist can be used for the drupal modules. So if you have rules, for example, for drupal8, you can include it in that. So you have drupal-rules. And then if rules depends on a module, it can pull in from the main Packagist website, which was from GitHub. So I'm not sure how it's going to work precisely, but that's going to work. Well, if you have any questions, just ask. So basically you create your Composer to JSON for your project. It's pretty much the same as before. So you have your name and your description, your keywords, your type of license, your authors, and then what version of PHP you require. Composer requires PHP 5.3 and above. So you can only use 5.3 and above. And it's simple, I'm using 5.45. So the syntax has to be exact. You can't have invalid JSON because Composer is very, very picky. If you have any typos or commas in the wrong place, it will basically throw a tantrum. And to do that, you can basically run Composer Validate. And what this does is it validates that your Composer file is correct. So before you can meet all your files, you just run Composer Validate and it'll say this Composer is basically valid. So to submit your new library, you basically go to Packagist, create an account. If you have a GitHub account, it has OAuth so you can just do a single sign-in. You can basically submit any repo URL so you can do it from Drupal.org. You don't have to use GitHub. So if you have a repo on Drupal.org, you can basically submit it. Yeah, so you can use Git, SVN, and McAriel. But what if your code is an open source? So for example, what if your company wants to be using Packagist, but you can't because everything is all closed source? Well, it's fine. Packagist.org is open source. So you can basically clone it and then create your own private Packagist repository yourself and you basically create a new line basically says use this server rather than packages for these projects. So it's quite awesome actually. Yes. So you keep using GitHub or other? Yes. Yes. So on GitHub it's probably? Yes. And the dependency? Yeah. Yeah, but if you have multiple repos, it's a lot easier to actually just create. And it's easier if you don't have to? You're going to have to, yes, of course. Yes. So yeah, if you want to, that's fine. But if you have quite a few, then it gets really annoying to have to find an each line. So, but yeah, that's your large. So what if you have to put it all but like it goes to offline because the way Packagist works is it pulls in from GitHub and drip on sort of stuff. There's actually a new thing that came out called Torrent Proxy which is free for personal use and it's more free for commercial. What this does is it basically proxies your composer through your own local proxy. So basically when you pull something through it'll download off that. So if you're on a plane or offline, you can basically keep a little approach on your computer. It's created by the creator of Composer. So pretty much any money towards that goes towards commercial development, which is great. So yeah, let's all work together to create better PHP libraries. As you probably know, there's like multiple CMSs, multiple frameworks doing different things. And it's all pretty much the same project. If we can share code, it'll be a lot easier because now we can pretty much just like do a composer require of a certain library, pull it in and then multiple people from different like Drupal or Magento or like Symphony, like, you know, all work together rather than having to like have the wrong way of doing things. The nice thing about Drupal 8 is it spun off a lot of new PHP libraries. So for example, with Commerce 2, there was, there's quite a few new libraries that you can use for PHP. So even if you don't use Drupal, you can certainly put in those libraries inside of your own project. For example, one of them is called, it's called addressing. So basically, as you're probably aware, inside of Drupal before about two months ago, if you had an Australian address field, it wouldn't have personal code. It had something else, I don't know what it was. But now thanks to, yeah, it had zip code. So clients will ask me, what does it say this instead of saying the actual formatting? And there's like 200 different countries. So every single country has got different formatting. So basically what addressing does is it basically solves this. So for example, you can have subdivisions for 40 countries. You can have different formatting. So it has the proper Australian formatting now, which is great. You can have like, if your customer's in a different country, you can set it to them as well. So when you choose your country in your address field, it'll pull through all that, which is great. And it does postal formatting for you as well. So say the customer answers in address one way. You can then say, hey, I want to have this address formatted to be in the format of New Zealand or Australia or what do you want to have it as. And this is using the Google address data service, which is basically a open source JSON format that Google has created. The next one is called commerce guys tax. Basically, this library is a, you can use this to generate taxes when you're trying to sell something. So for example, save, there's a lot of tax change in a country. For example, save changes from nine to 21%. How do I calculate what percentage tax needed to sell it as? For example, save the item was processed on January, sorry, like in December. And then the tax rate changed in January. You need to figure out if it's at 19% or 20%. So you can use this library to do that. It has predefined tax rates for countries to collect taxes and countries to sell in. There's currently an Australian GST pull request that I needed to create to actually create GST. So yeah, that's coming soon. The next one is pricing. Basically, what pricing does is it formats the price of any currency and location. So save you pass a number like 10.5123. It'll format as $10.51 Australian because we don't really use anything past two. And you can also tie that in with another library called Swap, which basically you can do currency conversions. So save you have $10 which you want Australian. You're going to convert that to USD. You can do that using the library or any other currency like that. Another one's called Commerce Guy's Zone, which is basically zones of territory, groupings you mostly use for shipping and tax purposes. So basically you can group up a whole bunch of different areas and then call that a zone. For example, you can do suburbs in Australia because multiple suburbs have got the same postcode. So you can group all those suburbs up. So save you want to say, I want this zone to have $10 shipping or this zone needs to have $20 shipping. You can do that on a per-stop basis, which is great. And it makes definitely pricing easier for shipping in e-commerce. So you can do like, yeah, quick thank you. Any questions? Yep. Yeah, that's hard to answer because it's like everyone's going to have their own way of doing things. And I'm sure people want to still use Rushmake. People still want to use Composer and Stool. There is a work in progress for that with 8. So basically you can define your patch files and then Composer will actually install those for you. But that's still a work in progress. So I'm not sure if I'll end up being an intuable Composer or what's going to happen with that. Hi, yep. I've been playing with the manager a little bit and I was wondering if anyone actually has any experience with... I've been using like the auto loading the bottom. And I was just wondering like, isn't doing lazy loading, like I haven't looked deep enough. Yeah. Yeah, because I just figured it's... If I'm doing that everywhere, is it going to just... No, that's why you use... Yeah, see, I'm writing it in that. Because if you don't use it, even if you do simple, simple, simple product, it's in an instance of something, you won't actually be able to pass the full file. It's not accepted directly into your text or create an effect for a human class. Is that even... Even though it's a file of... Do you think it will be... Like, do you think it's just being... It's mapped to a... To a final name and you will... With those effects, it's quite a... Yeah, it's not like that. Yeah. Which is what Drupalight uses. So basically when you use the... I use time in Drupalight, you can basically include the library that way. So... Yeah, yeah, that's kind of what I'm... Like, this is in 7, and I'm trying to... You can use that in 7. All that same sort of standards, and that's... I'm just kind of getting into it, so it's running with the pitfalls or the thing, but... Yeah, because basically you just use use, and then the nice base, and then you can include that way. And that's... Yeah, a lot better than... Yeah, see, that's what... Like, I spent hours figuring that one out. But it's like... Why isn't it working and going through... Trying to figure out where it's trying to load the library and that was actually the problem with kind of the build build. Yeah, the XR load, what will it do? The XR load. Yeah, but see, the difference is, because that... I think you need to have... Or maybe Drupalight... The vital name for that to work. Like, what I'm doing is... Actually creating my own namespace directory, so you don't conform to the autoload format. So that's why this is... You're not conforming? Yeah. Yeah, it's not... It's a little different from the XR load, because I use that as well. Again, this is in the previous model, you can... Overall, part of the rules... Or if you don't find something, it's just... That's kind of the difference. So if you find the first rule, so you can go back and do it. So if you have your own name, you can link it. You can put it on Google, but you have it now on Google. So you can load... Do your own rules on Google. But you can try it with a SQL script, without... Outside of Google, just because you can load security... I have questions? Oh, yeah. No, it's... This is a contra module, that you can just switch off for later. But it... Can you switch off for later? Yes. That's a very good question. What Compose Manager does right now, it basically... When you CD it into core, you should... Like, in my opinion, you should never touch that Compose.json, which it does currently. They're looking for a solution to fix that. I'm not sure what the final solution is going to be for that, because ideally, I never want to touch core, the core Compose.json, or the root directory Compose.json. So I'm not sure how they're going to solve that one. Hopefully, they'll come up with something, but I'm not too sure what they're... Yeah, yeah, yeah. Yes. But, hopefully, your basic... And the module, it puts it onto the correct directories. Yeah. And everything like that, and you just run it, but it's quite good, because, basically, you're... The basic repository that you built to do it is 240k. Yeah. And it's basically just... It ends up being just your work and nothing else. Yes. That's another good point, is it like... When you do Drupal 8, deployments, for example, like your repo could be less than a meg, and they just do a Composer and Store on production, will that pull in all the Drupal 8 stuff? Or had you... Well, when you... Yes, that's what I'm saying. Yeah. And it will just download everything in the version that you use in Drupal 8 as well. That's what I'm saying. It's going to be really interesting to say how people handle that, because I think we're going to commit everything like Drupal Core does, because what Drupal Core does is they commit every single library, so all the dependencies are all actually committed. Is that how people are going to handle that for Drupal 8? Deployments for their own custom sites? I don't know, so... I think you would just go... You load in the new feed changes and... Yeah, I'll let you go. ...update.db, and then you're live. Yeah, that's what I'm planning on doing for 8, so... Oh, okay. I haven't actually done that yet, so I'll just look into that. Yeah, it's still really heavy in development, so I don't know where it's going to end up for this Composer Manager stuff. Yeah, it'll probably have... Oh, absolutely. Absolutely. Like you have Drupal Slash and then... Now have your own... Yes. So, basically, when you... When you have a library to find it, you can just adjust it into be Drupal Slash, and then whatever the name is. Yeah. From memory, it creates it inside of the core folder. So, basically, where core is, it has a... Inside of core, there's a protocol vendor. Basically, that has all the Drupal 8 dependencies, and then it will put your dependencies in there as well. I'm not too sure about 7... Seven weekends at the normal... ...sites of libraries. Is that the sort of vendor? I use my own class a lot of stuff, but I haven't really touched it much on Drupal 71ers, so I'm not too sure. Any questions? Well, that's it, I guess. It ended a bit early, but... Cool. Thank you.