1

Topic: OOP version for Composer

Would it be possible to also make the oop version available via composer? I think this would be beneficial as the standard behaviour of composer is to load functions on every page load. I.e. htmLawed.php gets loaded on every page even if it is not used. Using http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/class/htmLawed.php, composer's autoloader would only load the class if it is actually called.

2

Re: OOP version for Composer

Thanks for this suggestion. I will look into implementing it – should be easily doable.

3

Re: OOP version for Composer

I have added the OOP (class) version of htmLawed as a separate package (htmlawed-class) in packages.json. Instructions for using Composer to install/update this version of htmLawed are here. The htmLawed files get installed in vendor/htmlawed/htmlawed-class, as against vendor/htmlawed/htmlawed, which is used when Composer installs classical (non-OOP) htmLawed.

Can you kindly check if this works as intended? I am not familiar with Composer, and I am not sure if the autoload specification that I have put in packages.json is the best.

4 (edited by walrusmoose 2019-11-06 15:35:21)

Re: OOP version for Composer

Thank you for the fast reply and implementing this.

After running:

composer require htmlawed/htmlawed-class

I ran into some minor issues that might require some small additional changes to the class:

First, due to: 'Class 'htmLawed\htmLawed' not found'

above:

class htmLawed{

I added:

namespace htmLawed;

Then due to warnings like:
'preg_replace_callback(): Requires argument 2, 'htmLawed::hl_tag', to be a valid callback'

I replaced all occurrences of 'htmLawed::' with 'self::' which from my reading should be the same (but see: https://stackoverflow.com/questions/3481085/self-vs-classname-inside-static-classname-methods-in-php). Alternatively, the namespace could be specified in full, i.e. 'htmLawed\htmLawed::'

After the above minor changes, things appear to work well (autoloading as expected---called as normal, though now specifying a namespace):

 
use htmLawed\htmLawed;
$out = htmLawed::hl($in, $config);

Perhaps "psr-4": { "htmLawed\\" : ""} could be more specific as "psr-4": { "htmLawed\\" : "htmlawed-class"}. Though I don't think would make a huge difference as the array key/value pair in the composer generated 'autoload_psr4.php' already looks good to me:     

'htmLawed\\' => array($vendorDir . '/htmlawed/htmlawed-class'),

5

Re: OOP version for Composer

Thank you for testing, identifying the issues, and suggesting the code changes to fix them.

While looking into this, I found that the issues don't arise if autoload is specified with classmap instead of psr-4.

I have therefore modified packages.json to use classmap. With this, code in htmLawed.php does not need to be changed. Also, with psr-4, PHP version requirement would have been 5.3+.

6

Re: OOP version for Composer

That works, thanks!