New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Composer #50
Comments
What exactly do you want to use Composer for? Like to publish to composer as a package? To state that it depends on twig and dev-dep on phpunit? or all the above, etc. |
good question; a few people asked about adding Composer which is what I was reacting too. My process doesn't involved Composer so I wanted to familiarize myself with it and add the necessary pieces. Do you already know it well? |
Yeah, I've used a Composer when I was working more heavily in PHP. In a nutshell, it is sorta similar to Rubygems, in that Composer manages dependancies of your project (Gemfile) and generates an autoload for you (Bundler manages load paths in ruby). Honestly, I don't see the benefit of adding it as a package to composer. The cms handles updates, installation, and loading for you. Maybe for development, just adding a composer.json file to manage the dependency on PHPUnit and Twig? I would hesitate on twig though, since it is a core dependency, and not all people using your plugin are composer-savvy. PHPUnit on the other hand, is a developer tool and should probably be managed. |
NPM (node package manager) is similar as well, except NPM loads modules with CommonJS's require function. Composer just auto-loads with the PHP SPL |
Composer works with Wordpress (see WPackagist ) but the main file ( |
@Anahkiasen can you send attach a sample of what that |
Of course yes, thanks for the wonderful package by the way :p |
@Anahkiasen do you feel WPackagist really helps your workflow? I think Wordpress lacks a inter-plugin dependency system, and this would sorta solve it. How does this compare to using something like WP-CLI? I ask because I am brand new to Wordpress, so I probably haven't run into a lot of the problems veterans have. |
My 2c: in WP development, I use composer mainly for generating an autoloader and sometimes for non-wp dependencies (like loggers, or in this case, Twig). But if you ask me, once you go PSR-0, you never want to go back.. Simple example of what this would entail for Timber: // composer.json
{
"repositories": [ // Define repo for PHP Router ],
"require": {
"fapbot/twig": "1.14.0",
"dannyvankooten/PHP-Router" : 'dev-master',
"asm89/twig-cache-extension" : '*'
},
"autoload": {
"psr-0": {
"Timber\\": "src/"
}
}
} The file structure would then become something like:
One gotcha is that the check for PHP version should happen in the main plugin file, before the autoloader is included, as namespaces only come in at PHP >= 5.3.0. Another is that this entails that all classes will be namespaced.. so anybody currently using (or extending) TimberPost, for example, will be in trouble and has to change to \Timber\TimberPost. One solution for backwards compatibility would be to include all those classes the ol'-fashioned way - but that kind of defeats the purpose. Another would be to create class aliases for all those classes, so at least in a couple of next versions, the root-namespace classes can still be used. |
They don't have to be namespaced, Composer supports all types of autoloading besides PSR0. It's ideal in most case but the Wordpress community isn't used to namespaces (and a lot of other things PHP now uses) so I'd say it'd be best to use a |
You're right about alternative autoloading @Anahkiasen. And yes, in WP namespacing is still quite uncommon, also because WP still supports PHP5 <= 5.3. But since Timber already required a PHP version that supports namespaces and uses modules that are namespaced, why not use namespaces and PSR-0 for everything Timber? Maybe a solution is to add (yet) another autoloader that creates aliases on-the-fly for classes that start with 'Timber'. So Or are there other reasons for sticking to non-namespaced classes? |
Well if you add an autoloader like that you're basically having unamespaced classes but with worse performances. It's not that much of a problem as classes are already "name"spaced (ie. |
Yep, performance of "name"spaced classes will suffer with class aliases in an autoloader - but this would just be a temporary measure to ensure backward compat with themes that use the current classes. The current "name"spaces, however, do solve most of the problems that namespaces should solve as well. So keeping them is a valid option. Developer ignorance is a good argument for sticking to "name"spaces. However, it is a double edged sword: a couple of years ago, most plugins were completely functional, and using OOP could have been considered harmful, as most WP tutorials out there preached the use of functions. Some plugins unfortunately still do this (I'm looking at you, Akismet). It's a tough choice, and a matter of taste. But considering that Timber is there to advance WP development and bring it up to standards that are considered default on most modern PHP projects, I'd still lean towards namespacing all of it. (Ps. @Anahkiasen, off topic: I didn't know of any plugins breaking when using namespaces.. any idea why that is?) |
Well I'm talking about plugins breaking when the user's classes are using namespaces, in the case it was the JSON API plugin if I recall. You do make a good point about Timber being kind of a "top of the lot"/outsider in terms of the PHP knowledge it requires and teaches its user (which is why I used it in the first place). Hadn't thought about that. I think that kind of changes it yeah, when you see it in that light maybe namespaces would be a good thing. |
Most hosting providers seem to give PHP 5.3+ support, including the popular ones shown on wordpress.org. Namespaces breaking stuff is an edge case that can/should be tested for (I am learning PHP Unit right now so I can contribute a bit on this front). IMHO it's better than what I consider the dumb approach of "prefix all the things" function approach... But thats another rant 😉 - http://i.imgur.com/ua7Vgg3.jpg I agree that Timber is sort of an outlier when it comes to WP development. I stumbled upon it when searching for twig templating for Wordpress. Chances are, unless a dev has experience building non-trivial, non-wp applications, they probably don't really come at things from the MVC/logic-view separation mindset and do what most tutorials show. And the devs that do happen upon Timber who don't know concepts like PSR-0 or namespaces, will come out a better dev learning about it. I'm all for pushing the PHP community forward where possible. On a practical note though, from the public API, these things will matter very little. The |
Hey all, I'll confess I'm a little new to this world. I think I'm one of those WP heads who wants to do better than In terms of the organization tough; that's a piece that I'm really interested in learning more about the various options that will keep this playing nice with things like Composer and other dependency managers, etc. |
Ideally composer would be installed through packagist while the timber plugin would be installed through the wpackagist [http://wpackagist.org] and the two communicate. That way if WordPress is working beside a separate instance of twig (Silex, Symfony, etc) then you can use a single library and just add in the plugin. |
@mtsprankle I think I follow -- did you mean that Twig (not composer) is installed through packagist? |
Twig would be added as a dependency of Timber yes, and kept up to date like that. |
Doing some clean-up. For composer users, does the current setup satisfy? I posted to packagist a few months back and it's being (sporadically) downloaded. Anything else here to worry about? |
I think Twig and Twig Cache should probably be included as dependancies of Timber. I don't think it is from looking at the composer file. |
I pull requested some tweaks to For "serious" conversion I would say Twig and any other dependencies should be converted to such in Composer terms. However that makes Composer primary install method rather than checking out repository, so depends on your goals and target users. |
They can be added as dependencies via Composer and then track |
So I'm taking this on this week to close this up. The big thing is to separate the code here on GitHub as the composer-fied development version and the WordPress.org version as the fully-packed one-click development version. Here are the steps I'm going through:
There might be more composer-related changes to make down the road, especially related to autoloading, PSR-0, etc. I think those are best handled as separate forthcoming tickets. |
I have this pretty much ready. The only exception is the CacheExtension from @asm89. It's loading in via composer, however I didn't fully understand how your autoloader functions together with other files. @mgmartel, can you take a minute to refactor so it pulls in the composer version? https://github.com/jarednova/timber/tree/50_composer ... I think this is following the correct pattern for composer loading. But if you have an alternate recommendation I'm all ears. Once this is complete, I hope to move-on to more advanced stuff with PSR-0, etc. (which I can see you already have going in parts of the cache). |
@jarednova Can you open a PR with your changes? Makes it easier to comment/add suggestions. Didn't know that this plugin uses my cache extensions btw, cool! :) |
Hey @asm89 welcome to the party! I don't think there are any actual changes, more of an implementation we're trying to get right. I'll defer to @mgmartel if there is something to PR back to you though -- he worked on the caching elements of the code. Thanks for lending your code! I'm really hoping to popularize Twig with the WordPress world. |
@jarednova I meant with your changes to get composer working with this repository (since you're asking feedback on https://github.com/jarednova/timber/tree/50_composer). A PR would make it easier to comment and provide feedback. It seems you're doing a decent job popularizing Twig in the WordPress world. :) |
This is now all pulled into |
|
Hey @Rarst thanks for the feedback....
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)){
include_once($autoload);
} else {
trigger_error('you need to run composer install first');
} Three. done! |
Not sure what you mean. If header on top (before issues and wiki) that's pretty informational, it's just for human reference. It points to master for me right now. On 2 — yes, sans "else" part. If you create Timber as standalone project autoload would be in |
Awesome, that's good-to-go. In terms of the packagist question, here's what's featured on there now since I pushed a patch to a branch for an issue: ... you can see that source is now listed as |
I imagine you can probably specify that explicitly in support section of |
cool, the rest of your items are taken care of. Calling this one closed |
* master: removed "public" keyword -- methods in question are public by default fixes #230 added link to TidyRepo adding test for short codes close #125 added source to composer.json as recommend by @Rarst in #50 test for autoload file before inclusion from composer ref #50 changed self to TimberImageLibrary instead of action hook
No description provided.
The text was updated successfully, but these errors were encountered: