Skip to content

Commit

Permalink
Alternative fix to Composer autoloading issues.
Browse files Browse the repository at this point in the history
It may be possible to overcome the limitations of the "files" autoloading mechanism by changing the file name in every version.
  • Loading branch information
YahnisElsts committed Oct 8, 2019
1 parent 66472c5 commit 2a7c818
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Puc/v4/Factory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
if ( !class_exists('Puc_v4_Factory', false) ):

require __DIR__ . '/../v4p8/Factory.php';
class Puc_v4_Factory extends Puc_v4p8_Factory { }

endif;
37 changes: 1 addition & 36 deletions Puc/v4p8/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,39 +294,4 @@ public static function addVersion($generalClass, $versionedClass, $version) {
}
}

endif;


/* We need to load the major-version and minor-version factory classes at the same time if possible.
* If we don't, an older version of the library could register the major-version factory later and then
* that older factory wouldn't know about this version. That's why we put both classes in the same file.
*
* This could be handled more elegantly with a custom autoloader or a simple script that just loads our
* dependencies, but we need to support the Composer autoloader. Unfortunately, the "files" autoloading
* mechanism has its own issues. See:
* https://github.com/YahnisElsts/plugin-update-checker/issues/300
*/
if ( !class_exists('Puc_v4_Factory', false) ):
class Puc_v4_Factory extends Puc_v4p8_Factory { }
endif;

//Register classes defined in this version with the factory.
foreach (
array(
'Plugin_UpdateChecker' => 'Puc_v4p8_Plugin_UpdateChecker',
'Theme_UpdateChecker' => 'Puc_v4p8_Theme_UpdateChecker',

'Vcs_PluginUpdateChecker' => 'Puc_v4p8_Vcs_PluginUpdateChecker',
'Vcs_ThemeUpdateChecker' => 'Puc_v4p8_Vcs_ThemeUpdateChecker',

'GitHubApi' => 'Puc_v4p8_Vcs_GitHubApi',
'BitBucketApi' => 'Puc_v4p8_Vcs_BitBucketApi',
'GitLabApi' => 'Puc_v4p8_Vcs_GitLabApi',
)
as $pucGeneralClass => $pucVersionedClass
) {
Puc_v4_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '4.8');
//Also add it to the minor-version factory in case the major-version factory
//was already defined by another, older version of the update checker.
Puc_v4p8_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '4.8');
}
endif;
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"php": ">=5.2.0"
},
"autoload": {
"files": ["load-v4p8.php"],
"psr-0": {
"Puc_v4p8_" : "",
"Puc_v4_" : "",
Expand Down
28 changes: 28 additions & 0 deletions load-v4p8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
require dirname(__FILE__) . '/Puc/v4p8/Autoloader.php';
new Puc_v4p8_Autoloader();

require dirname(__FILE__) . '/Puc/v4p8/Factory.php';
require dirname(__FILE__) . '/Puc/v4/Factory.php';

//Register classes defined in this version with the factory.
foreach (
array(
'Plugin_UpdateChecker' => 'Puc_v4p8_Plugin_UpdateChecker',
'Theme_UpdateChecker' => 'Puc_v4p8_Theme_UpdateChecker',

'Vcs_PluginUpdateChecker' => 'Puc_v4p8_Vcs_PluginUpdateChecker',
'Vcs_ThemeUpdateChecker' => 'Puc_v4p8_Vcs_ThemeUpdateChecker',

'GitHubApi' => 'Puc_v4p8_Vcs_GitHubApi',
'BitBucketApi' => 'Puc_v4p8_Vcs_BitBucketApi',
'GitLabApi' => 'Puc_v4p8_Vcs_GitLabApi',
)
as $pucGeneralClass => $pucVersionedClass
) {
Puc_v4_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '4.8');
//Also add it to the minor-version factory in case the major-version factory
//was already defined by another, older version of the update checker.
Puc_v4p8_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '4.8');
}

6 changes: 1 addition & 5 deletions plugin-update-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
* Released under the MIT license. See license.txt for details.
*/

require dirname(__FILE__) . '/Puc/v4p8/Autoloader.php';
new Puc_v4p8_Autoloader();

require dirname(__FILE__) . '/Puc/v4p8/Factory.php';
require dirname(__FILE__) . '/Puc/v4/Factory.php';
require dirname(__FILE__) . '/load-v4p8.php';

0 comments on commit 2a7c818

Please sign in to comment.