 Okay, hi, I'm Josh. Thank you all for coming. I really appreciate it This talk in a more abstract sense than being about composer as it says on the sign is about writing better more manageable code It's about writing more reusable code which equals better code and it's about writing It's about writing Code that's more decoupled and therefore leads to better reusability leads to better code And it's about reusing your code better, right? We have GPL. We're all writing GPL code So let's reuse our code between client sites between our own projects between other things that we find Let's make it very easy for us to reuse what we're doing so that way when we work on one project And we make an improvement we can instantly have that improvement for everything that we do and therefore we're writing better code We're better developers That's what this is about in the abstract In that can apply to most package managers dependency systems today. We're talking about composer at the PHP dependency manager Before I go any further. I do want to say This talk is not about WordPress core or what WordPress core should do. I personally don't think composer has any place in WordPress core And that's also just the reality, but we're talking about making plugins making themes making sites for people using WordPress That's what we're talking about so And that also means that we don't have to entirely follow how things work in core in a sensible fashion. So I Do a lot of WordPress things And I have a company called erative P. We do WordPress plugins. I'm working on a SaaS service powered by WordPress I have a site I write for a lot of people and I also work for pods the free content develop Development system. So we let you do Custom content types custom fields very easily I do lots of things and this is important because I'm probably working on 30 or 40 plugins right now And I'm sharing a hell of a lot of code between them And I mean the process of as much as possible moving this into composer so I can more efficiently share between So this is a topic that is very near and dear to my heart So what is composer? Composer is the PHP dependency management system get composer org I'm not gonna walk you through installing composer. It's gonna be slightly different by Depending on your operating system, and I've never done it on Windows, but really At least for you know, it's like operating systems. It's gonna take you about three minutes, and it's gonna take two commands It's very easy read their docs. They're great But what is dependency management that you speak of? a k a package manager. I'm gonna use these terms in in interchangeably today a Package manager dependency system it automates installing your code Updating your code removing your code, right? I no longer need this dependency this package or I want to deploy code Because your composer Will is probably going to be part of your get repo for your site for your plug-in for your theme Which means that it can exist in your local site it can exist on your? Development site it goes on your staging site it on your life site together And so these things can be used as part of an automated deployment process So why do we care? Why is any of this important to you as a developer as a plug-in developer as a theme developer as somebody who's making Wordpress sites for people or for yourself One big reason staying dry Dry as the do not repeat yourself principle This is a mis-sacred principle that we must all abide by I Love wordpress. I Feel like there are some things of wordpress that are anti the dry principle That are amongst our practices that we can do better on and using systems like Composer can help challenge us To be more dry and do better and write better code So the idea here is write code once and reuse it right or just don't read write code use somebody else's code Right we can do better work. We just use somebody who's better than us Like why should we be in an open-source ecosystem if we don't take advantage of it? Like if there's some better way of doing something besides writing all this code again Why the hell wouldn't it wait? And also no new coffee pasta who here has like a really good Has a really good bit of code that they've cut and pasted from one project to another and like renamed the class name Yeah all day I Am trying as hard as I can to eradicate this coffee pasta process for my life because it works really well right you copy from one site to another and You do a find and replace to like change this class prefix or whatever Is there and then now you're good and then you use it and then you find What's wrong with it right because it's code. There's always something wrong and right This is just an assumption that we should work with and So you change it and then you realize that like this is cut and pasted from another project So then you have to like cut and paste it back to that other project and hopefully you like a do that and be You don't make a mistake in that right because now the your line numbers aren't exact And also you've made some changes like you have divergence that's happened from the find and replace And what you're doing now is you're violating the separation of Concerns between humans and computers computers are really good at doing things predictably over and over again which is something that humans are bad at and Humans are really good at being creative and coming up with new and interesting things and that's something that computers are bad So when we get away from copy pasta We can we can fix that division of labor We can let computers handle moving seven copies and keeping them in sync between seven projects And we can focus on being creative and that's what makes us good developers is when we're creative and what we let the technology Do its thing get out of our way. I'm big on automation I'm going to resist the urge to talk about like current all the things in here because it's not on the topic But I'll be at the happiness bar most of today and tomorrow so So yeah, we use your code right update it once and have it update everywhere Let's be smart about our reuse. That's really what this is is that? computers one used right can enhance our intelligence and that applies to both programming and you know Google storing all of her information that we can't remember. So let's uh, let's you go. It's good Let's let computers help us be better at this other dependency management systems PHP is the Composer is the PHP dependency management system There are plenty of them out there that you might know and use The node package manager very helpful one for node JavaScript And also gets you into things like Bauer in gulp these things Which are very useful especially for front-endy stuff Pear probably not incredibly useful for most of us App to get in there a bunch of similar ones app get is the package manager for several flavors of Linux and Other flavors of Linux have a similar system. So that's more of a generic term Get modules some modules are sort of a dependency management system, but um, they suck So whatever, but they they can work in this way And let's not forget the WordPress plugin and theme admin system. That's a dependency management system And in a lot of cases, it's good enough. Let's not forget that that's an option that we don't always need to go down this round But also helps you conceptualize what a dependency management system does it lets you say on this site I have these nine plugins. It says I'm going to update this plug in now. I'm gonna remove this plug in I'm gonna add a tenth plug in So in same with themes it helps you manage all these independent packages that aren't part of WordPress So you even if you've never used Composer you've never used MPM. You never use gulp. You never use Bauer Everybody here uses WordPress. So you've all used a dependency management system It's limited. It's not going to solve all your problems, but you've done it So uses for composer in WordPress Improving deployments you can use Composer to build your entire site And we'll get into all of these in more detail Plug-in theme and site development share common code between plugins and themes and use your code in other people's codes In different in your projects in across projects. These are our general categories. I'm talking about And I'll go in each of these in detail Before we go too far though version control systems VCS for short or get for short Versus dependency management because you're all if you're new to this There are no other dependency management or the version control systems that make sense. So we can just call it get the This is a question that's gonna come up and it's a this is a healthy question. Why can't I just use a git repo? You you're going to but we're gonna talk about your git repo for a project in project Could be a site could be a plug-in could be a theme versus what's going in Composer Because you should use both right. I know me and saying you're not gonna use get because that would be insane This is a terrifying world that keeps us all the night So When do you use a V what what to use a VCS system for a version control system? Um, is it your code and is it unique to this project? So those are two things right if it's not my code, right? I'm using easy digital downloads on a lot of things right now I'm not version controlling easy digital downloads. It's not my problem. That's their problem Let them deal with it. So that is why I use dependency management is to pull in easy digital downloads and easy digital on-the-board software Licensing these sorts of things It isn't unique to this project. Um, I I dog food a lot. I'm a huge believer in it. So a lot of times I have a Site where half of the plugins are my plugins still they don't belong to this project Therefore, they don't they get managed by a composer But if it's unique your configuration files Your structure sometimes your index.php for your site These sorts of things your WP config That goes right in your git repo. Oftentimes I put the child theme that I'm using for a site in the git repo for that site And that's pretty much the only thing that I'm ever committing on unless I need to make a change to WP config or what's included to WP config Everything that isn't unique Everything's not unique to this project. It could be reused to use a dependency management. So this is basically all the things It's not all the things means basically all the things and that's the unique the distinction is uniqueness to what I'm doing in this project Even if I'm writing it for this project, it might not qualify as unique to this project because I'm probably going to reuse it And don't forget composer checks packages out as a version control repo So it's managing the process of checking things out from github for you If you're if your package is coming from it is coming from a version control repo, which is Almost all the time. So if you make a change, right? You're dogfooding. You're using the plug-in that you developed for a site You're using a library that you've developed for your plug-in And you make a change to that in the context of that site, right? This was the whole point is that we can reuse all this stuff Go into that repo Get commit, you know get add get commit get push you've now updated that repo every one of those packages is a get it is Get and you therefore can make changes commit them and now they're back up on the internets and you can pull them down into your other projects So composer json. This is how the composer repo Composer project is configured is in the spot Important part is dot json This is a json file it must observe the json syntax if your php developer This is going to annoy you because like you can't put a comma at the end of a of a key in an array. I will Give a prize to anybody who finds the errors in my examples here But also it makes cutting making pulling examples out into articles and presentations terrible Because if you want them valid json You have to do things like this But if you just copy this block in to something that's existing the fact that there's not a comma here is now going to create the invalid json Because the next thing needs a comma, but I don't know so in its own. I don't put a cop This is why you should be using an IDE that has syntax highlighting in my opinion php storm, but when I also a Lot of my examples that I'm doing they are pulled from they're all pulled from the real world And those are github links So I'm going to walk you through a example Composer dot json for a wordpress plugin. This is a pods plugin I work for pods that I had nothing to do with But this is the Meta info this is at the beginning. This is telling us What's the name of the package in that's vendor slash the package name? Give it a description. This is generally the same as your plug-in or theme description What type this is the most important piece in this entire thing? Composer by default treats types all packages as a library if you're making a wordpress plugin or a wordpress theme It's aware that that's a thing But you have to tell it that this is a type wordpress bug This can become very important later when we talk about site development But you always want to make sure you say wordpress plugin or wordpress theme If it's a wordpress plugin or wordpress theme because composer can customize where things go We'll talk about where things go and by default they do not go where you want a wordpress plugin to go keywords Useful depends on really what you're doing. It's gonna be listed on packages. This is really helpful home page, right? License, you know dropping gpl there Authors and this gets clipped off as they said that link if you want to see the whole thing Just a list of authors who it is So people can see it. I actually chose this one I don't know if Naomi's here today, but she's a you know a vanilla person and she's contributed to this plugin on pod So it made sense to pull one that she contributed to and it's good because it allows you to acknowledge everybody Right. We always start with pods framework team and then we list who are the main contributors there and you'll see on this one Not including me And then you can also have a section for links That's really great for support where the issues go And where's my source code? These are important because if it's listed on packages, it'll automatically pull these and make them links on packages And also just some people looking at your code know where to go to complain to you Requirements this is the world's most basic requirements list But remember we're gonna get more into this requirements are what are the dependencies of the system? What is it that I need to install? So we're going to say In this case Composer installers that's sort of gonna have it all the time and then a PHP version And PHP won't be installed because it's going to in most configurations that we're in the world of this is more for reference But notice that this has a greater than or equal to What with all of these we can say greater than or equal to greater than these sorts of things comparisons for version numbers Composer does require 5.3 To actually function but depending on how you're delivering and we'll get into this You can use it if you observe the backwards compatibility of forward press We'll get into more of that issue. I've been talking about packages A package writing Composer package can be hosted in on packet on a packages server packages.org is a Repository of packages that you can automatically pull from of PHP packages W packages.org WP packages Is a mirror of the WordPress plugin and WordPress theme repositories on WordPress.org? So every single plug-in that's on WordPress.org has a package automatically generated on W packages.org This is very useful. We'll look at this in a minute But you can also just use any Get SVN or whatever type of VCS repo you're using So that's really helpful for like say your custom code and as long as the server that's running it has access to it so you have a public repo or you have a Repo that your server has the SSH key for If it's private on GitHub or BitBucket you can pull from there And that's fairly seamless like if you go into your GitHub settings and you Add an SSH key for your server It'll work The requirements I'm gonna think you threw a bunch of different examples here this requirement section This is really the money the author stuff the meta stuff not as important that type very important the rest of it It's very good stuff to have But like a plug-in header and WordPress you don't need to list the author you don't need to list the license, but you should So let's look at a couple of different types of packages here. The first one here is Coming from packages. We can see that because it just has somebody's in this case John P block who works for 10 up a Package in this package is called wordpress because by the way you can manage wordpress through Through a variety of strategies. This is my preferred strategy. It's really well configured I'll show you a little bit more about how this works, but I'm saying in this package. Give me wordpress 4.1 A plug-in from the repos the next example WP packages plug-in pot And then you'll see for version. I'm just gonna select star So that's the latest version and for WP packages. That's equivalent to pulling from trunk I'm sorry pulling stable Whatever the stable tag is the set in SVM and then this next one this is a example of a Package this is one that we wrote at my company. So we could use the Edd licensing Both like the UI for it So you can like click in and enter your license and also like the update or class from Edd that they tell you to put into all Your plugins instead of cutting and pasting in it all of them We just add this one line and it pulls from packages and now we have our UI rigged up And we have the actual class for running the update that you know That makes the API call to check that with the license and activates it where which one oh Yep, that is an accident. I apologize always quotes at the end. Yes, you win something Come find me afterwards. I will give you a prize totally legit. What? Yeah, I'll give you a free plug-in. How about that? I will totally come and pick up pick out a plug-in from our side. I'll give you a hundred percent code so The problem with Google Drive for its slides is that they don't have a syntax highlighter, right? And so I get totally dependent on a php storm to like tell me what I'm wrong Which is fine. This is the division of labor between humans and computers. I'm not a perfect at my JSON syntax, but a php storm will fix it for me also like the composer errors when you miss that, right? Like that's gonna throw an error Making a oh This one was wrong until about to about 20 minutes before my talk by the way, so I fix it And in this by the way, we're clipping off the eye. I kind of fix that In there like it's good. This one. I know is wrong, but it looks good here, so Oh early, so Google Drive So what if you have a plug-in that's on github that hasn't been released in this case? This is the pods json api Plug-in it's still in beta. We haven't pushed it out yet We need to rewrite it now that the core rest api has gone to 1.2 and If you want to use it in a plug-in if you have to make it known to your composer Dot json because it's not on packages. It's not a w packages So we have a repository section and we say what type is it it's get and then we say where is it? What's its URL get hub comm slash whatever and now we can use it in our requirement section just like any other It for this to work the git repo has to have a valid composer dot json in it You should anyway, so that was a little tour Let's try and put this all together with three examples that are actually going to match up with the three examples that we started with before Full site development. We're making WordPress sites This is my WordPress vvv start This is what I used before I automated the creation of it with grunt. There's a link to that too But it just makes this And my philosophy is that your git repo tracks your theme or your child theme. That's unique to this site um It tracks your WP config Though I have like I segment WP config out so that way I have one file That's just my database passwords and stuff and then I can just get ignore that so that way everything that I can actually have my Entire repo on github and not worry about it. There's nothing proprietary there As far as right I have like a premium bug. What does it matter? It's just coming in private github from a pre, you know private git or bit bucket For there, so I'm not like giving away somebody's copy of say WP migrate DB pro something. I always use I just maintain my own private repo. It's that way I can get it by dependency management I'm not being that jerk that's giving it away for free But my entire WP config can be there because it's got everything in it besides the database details, right? That's something that we can't make public and so That's getting ignored. I just drag it up with FTP. I only have to do it once who cares, right? But like my look if you look at here at the local database config, which is to work with VVV It's in there, right? You guys want to know the database password is WP on all like VVV who cares, right? in then move plugins This is going to depend on how you use must use plugins in general for me if I'm using a must use plugin It's unique to the site It's like add another in this case. The only one I think it's using is adding a new theme directory Because I get ignore WP and my well, let's not say WP content because I would never call it that My content directory slash plugins and slash themes and slash uploads But I do want my child theme to be included in my get repo So I just register another theme repo Because you another theme directory because you can do that with two lines of code in a move So that's unique to the site that goes in the get repo More full site examples Bedrock Julian raise your hand You guys know the roots theme. It's now sage. He's on that team They make this awesome system called bedrock that uses composer for dependency management uses ansible It's super sweet. It was a big inspiration for me, but oh, I must admit. I don't use it Okay, well he could connect you with the people who will But if you need like this works with capistrano for deployments, um, it is like the full nine And it's a very complete system. That's very similar It's in fact, I would say my system is like scaled down from there Yeah, they do good stuff like check out sage. How they is it? Also, as I said, I have a grunt in it template that can generate a unique For vvv and appointments site using the same example that I was showing there So example to I call this a very word breast composer package Um, and this is actually by mark jake was um, you don't know as a lead developer word breasts And he has this awesome soft expiring trash Soft expiring transient cache library Basically, it uses wordpress transients, but it's soft expiring. So if you hit it while it's out of date It'll serve you the out of date while rebuilding so that way your next one So the philosophy being that your um, your performance is more important than data being perfect Which works in certain situations and doesn't work in others It's on github and um, I'm gonna jump out into the github in a second, but there's um Rast Audrey has a awesome Case study on this as well as just in general information on Using at composer.rst.net on using composer with word press and also another cool caching system that you can use If I switch over to Here this is the This is the entire composer file for this So this and I have this linked in there But I would encourage you to check this out because the way that this works is php 5.2 compatible Is the most word-pressing example I can find and also it's like a really cool Package that you can use and like I wrote a caching plug-in for the REST API That has almost no code in it because I'm just using this for my actual caching And we're going to get back to this part here. Just keep that in mind. You open that one up. Okay Another example Creating a plug-in out of composer libraries, right? This is to me the end game is that your plug-in does almost nothing It's all in composer libraries This is one that we built that's on wordpress.org In by the way that helps answer your question about like well, how do I have right? I you download from github and now it doesn't have all the things I encourage people to in my read me to do a composer update when they download That's not going to track for all users. Well, that's where wordpress.org becomes your best friend Is that you can make a build? I grant automated but you can do it manually zip it up Send it to wordpress.org or put it in a you know, put it on a file on your server So you can still have a pre-built Uploaded to wordpress and it works plug-in has all of your dependencies included and it And it still has the ability really with this our goal with this was to it's a templating system where we are Using a templating engine. That's the php port of handlebars right handlebars to JS is very popular very nice JavaScript templating system We're able to We're able to load use that so we don't have to write that handlebars dot php somebody else already wrote it and We basically just give you a wordpress admin interface for this templating plug-in if it now We're able to use I've done it on a couple client sites and we sum up Upcoming plugins where we're not going to take the admin We're just going to use our classes that allow you to render it and you hook up a new admin to it But all of the logic of like let's hook this up to the content. Let's hook it up to a shortcode Let's hook it up to a function All that stuff we can just pull right in just with one line from composer Back up one second here. I wanted to show you before we go too much further on The importance when we talk about types remember I said if it's a wordpress plug-in you have to set type equals wordpress plug-in This is why it's important Wordpress installed there. This is very specific to John people ox wordpress thing Installer by a composer that it looks for wordpress installed there so we can say Where does it go in this case public dot underscore html slash wp wherever you want wordpress to go put it there Relevant in this is a rat. This is relative to wherever composer.json sits So public dot html is a subfolder of this directory slash wp. That's where I want In then installer pass this is working with composer installer Composers installer knows about a type called wordpress plug-in and it a type called wordpress thing If we don't do this our wordpress plugins and our wordpress themes are gonna end up in a vendor directory in the root of our Of our prop of our package and therefore wordpress doesn't know about them. So Instead we say installer pass for type wordpress plug-in. It's a little backwards how it reads We want them to go in You can think of that as WP content But I don't like to call it WP contact because I can't search for it I like to yeah in my case site will get replaced with a slug for the site so that way I can search from the Finder for my content directory Slash plugins and then just use the variable name sort of the package name That's what the folder is gonna be and then put it in wordpress plug It's a wordpress plug-in it goes into plugins for right we all know this and if it's a theme same thing site content themes Put it in wordpressing and so if you look at the requirements here Wordpress at the top It's gonna go into wordpress goes Most of these are plugins until the last two lines they're gonna go where plugins go because those all have the type Wordpress plug-in either because that's in their package or almost all of these are coming from WP packages So by it smart enough to do that and then at the bottom here You see these two are wordpress themes word W packages dust 2015 And then this Fukasawa theme which I use on my site that I like a lot And so those we've mapped at this bottom here That they go in the themes directory right so we're just kind of teaching composer What is the structure of my site? Where do I put a theme? Where do I put a plugin the composer auto-voter? In your um, you're getting a crash course in composer You're gonna have to ask you all that go in and read the docs They're very simple to figure out installing updating composer update these sorts of things, but very important This is one of the things that's great about composer is that it gives you an auto-voter There are three ways to use probably more there are three things that apply here PSR for in my opinion how you should be doing it Who here was for Tom's talk today on wordpress standards? I? Think he's really right But sometimes you've got to bend the rules because they make you allow you to work better And it's great if you could bend the rules into somebody into an established Standard so psr4 is a standard for naming and structuring your plug-in so that way it can work with a standard auto-voter your your package and It's not the wordpress way, but at least it's a way Right, so we're using the established php communities way of doing things Because I think that if we're plug-in development and sometimes for themes. This is a better way of working You know at least it's an established standard there. It can also support psr0. I don't really see the point there This is the older system PSr0 will work without a namespace so Therefore if you're concerned about php 5.2. I'm not honestly. I'm just not all of my plugins start with a Function that'll throw an error if you're on php 5.2 You like throw an admin error so that way it doesn't throw fatal error And then if not include a file that actually was the plugin and it's not 5.2 good battle why anybody would want to run on php before 5.4 doesn't make any sense to me And I'm done catering to them if you want to there's class map you can manually List out every class by path and it'll auto load it for you And we'll go through that PSR4 auto loader is very easy to use It's the best way as long as you're not stuck with php 5.2, which I realize many of us are We add an auto load section here and we say what is my namespace? And we end with a slash but we have to escape that slash so it's two slashes and then right so this is going to use the director the namespace hats and Then what is the root directory? In PSR4 your root namespace doesn't have to be in a directory called what your namespace is So we can just say in source. That's normally how it works And so now it'll auto load from there and then it'll pick up all your sub namespaces from there So if you have hats slash settings and that's in a sub directory hats source slash settings, it'll automatically auto load this You can do PSR0 without a namespace. I'm not really sure why You can also use the class map There are two really good uses for class map in file map Class map because in this by the way is copied from TLC transients This is how mark Jacob did it and this is very useful because this is PHP 5.2 compatible You just and it's not right. It's two classes in the entire plugins at the end of the world to have to write these And it's slightly less bad than doing like require the name of your class In the past what's really actually really useful about this is the file map There that way I have like a functions dot PHP in all my plugins for functions that aren't in a class So I use the file map to include a functions dot PHP Questions do I use go I have used go but I don't I'm a big grunt fan, and I know that makes me uncool but but There was a danger in the new shiny I Learned grunt and then I found out that I would be much cooler if I use gulp and The thing is is I spent a lot of time learning how to use grunt and grunt does everything that I need in that world if it works And I think in this kind of topic when you're talking about a system that good that's like I think that there are people who can approach the question of Composer or grunt or npm with the with the philosophy of this is the right way to do in every other way is wrong Right where PHP developers so therefore we have to use composer If you're sitting here and going this has no actual benefit to me Josh Thanks for your talk, but I don't get you know I don't not gonna help me with what I do then don't use it just because it's the right way of doing things if this is Something that's actually going to benefit you if everything that I've laid out here And you any part of it has an actual tangible benefit to you Then I hope this was a really good introduction to the topic and that you will you will see this as a start point And you will get into it if none of this really applies to you then great you learn something new move on with your life Shove it away in your head for later on my site. I have which is Josh press net There's an article that's linked to this That that has these slides embedded, but also it links to a Article of the same name that kind of this was based on and also some articles that I've written in for torque That have a very general introduction to composer just in general for WordPress and then there's one specifically for plug-in developers And I'm Josh and Josh press net which is a terrible way to get in touch with me I dream of a world where I don't never answer email ever again I'm at Josh 412 on Twitter Seriously, I'm always on Twitter And if you want to have a real discussion, I'll like tell you to DM me on WordPress slack or something It gives slack at Twitter how I actually communicate with people But seriously, thank you for coming and if you want to discuss this I'll be in this area over here with the happiness bar and I'll be on Twitter for the rest of my life So unless there are any other questions Y'all are awesome