diff --git a/algolia-index-js-searchpage.php b/algolia-index-js-searchpage.php index 716fa42..58b5986 100644 --- a/algolia-index-js-searchpage.php +++ b/algolia-index-js-searchpage.php @@ -22,19 +22,15 @@ define('ALGOLIAINDEXJSSEARCHPAGE_PATH', plugin_dir_path(__FILE__)); define('ALGOLIAINDEXJSSEARCHPAGE_URL', plugins_url('', __FILE__)); define('ALGOLIAINDEXJSSEARCHPAGE_TEMPLATE_PATH', ALGOLIAINDEXJSSEARCHPAGE_PATH . 'templates/'); - define('ALGOLIAINDEXJSSEARCHPAGE_VIEW_PATH', ALGOLIAINDEXJSSEARCHPAGE_PATH . 'views/'); load_plugin_textdomain('algolia-index-js-searchpage', false, plugin_basename(dirname(__FILE__)) . '/languages'); -require_once ALGOLIAINDEXJSSEARCHPAGE_PATH . 'source/php/Vendor/Psr4ClassLoader.php'; +// Autoload from plugin +if (file_exists(ALGOLIAINDEXJSSEARCHPAGE_PATH . 'vendor/autoload.php')) { + require_once ALGOLIAINDEXJSSEARCHPAGE_PATH . 'vendor/autoload.php'; +} require_once ALGOLIAINDEXJSSEARCHPAGE_PATH . 'Public.php'; -// Instantiate and register the autoloader -$loader = new AlgoliaIndexJsSearchpage\Vendor\Psr4ClassLoader(); -$loader->addPrefix('AlgoliaIndexJsSearchpage', ALGOLIAINDEXJSSEARCHPAGE_PATH); -$loader->addPrefix('AlgoliaIndexJsSearchpage', ALGOLIAINDEXJSSEARCHPAGE_PATH . 'source/php/'); -$loader->register(); - // Start application new AlgoliaIndexJsSearchpage\App(); diff --git a/build.php b/build.php index 2bb7617..e01b53c 100644 --- a/build.php +++ b/build.php @@ -6,15 +6,29 @@ } // Any command needed to run and build plugin assets when newly cheched out of repo. -$buildCommands = [ - 'npm ci --no-progress --no-audit', - 'npx --yes browserslist@latest --update-db', - 'npm run build', -]; +$buildCommands = []; //Add composer build, if flag --no-composer is undefined. -if(is_array($argv) && !in_array('--no-composer', $argv)) { - $buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev'; +//Dump autloader. +//Only if composer.json exists. +if(file_exists('composer.json')) { + if(is_array($argv) && !in_array('--no-composer', $argv)) { + $buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev'; + } + $buildCommands[] = 'composer dump-autoload'; +} + +//Run npm if package.json is found +if(file_exists('package.json') && file_exists('package.lock')) { + $buildCommands[] = 'npm ci --no-progress --no-audit'; +} elseif(file_exists('package.json') && !file_exists('package.lock')) { + $buildCommands[] = 'npm install --no-progress --no-audit'; +} + +//Run build if package.lock is found +if(file_exists('package.lock')) { + $buildCommands[] = 'npx --yes browserslist@latest --update-db'; + $buildCommands[] = 'npm run build'; } // Files and directories not suitable for prod to be removed. @@ -23,13 +37,15 @@ '.gitignore', '.github', 'build.php', + '.npmrc', 'composer.json', 'composer.lock', - 'node_modules', - 'package.json', + 'env-example', + 'webpack.config.js', 'package-lock.json', - '.vscode', - 'webpack.config.js' + 'package.json', + 'phpunit.xml.dist', + 'README.md' ]; $dirName = basename(dirname(__FILE__)); @@ -91,4 +107,4 @@ function executeCommand($command) // Return exit status. return intval($matches[0]); -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 9bb0a38..afba6d9 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,10 @@ } ], "require": { + }, + "autoload": { + "psr-4": { + "AlgoliaIndexJsSearchpage\\": "source/php/" + } } } \ No newline at end of file diff --git a/source/php/Vendor/Psr4ClassLoader.php b/source/php/Vendor/Psr4ClassLoader.php deleted file mode 100644 index f2a1971..0000000 --- a/source/php/Vendor/Psr4ClassLoader.php +++ /dev/null @@ -1,89 +0,0 @@ - - */ -class Psr4ClassLoader -{ - /** - * @var array - */ - private $prefixes = array(); - - /** - * @param string $prefix - * @param string $baseDir - */ - public function addPrefix($prefix, $baseDir) - { - $prefix = trim($prefix, '\\').'\\'; - $baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; - $this->prefixes[] = array($prefix, $baseDir); - } - - /** - * @param string $class - * - * @return string|null - */ - public function findFile($class) - { - $class = ltrim($class, '\\'); - foreach ($this->prefixes as $current) { - list($currentPrefix, $currentBaseDir) = $current; - if (0 === strpos($class, $currentPrefix)) { - $classWithoutPrefix = substr($class, strlen($currentPrefix)); - $file = $currentBaseDir.str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix).'.php'; - - if (file_exists($file)) { - return $file; - } - } - } - } - - /** - * @param string $class - * - * @return bool - */ - public function loadClass($class) - { - $file = $this->findFile($class); - if (null !== $file) { - require $file; - - return true; - } - - return false; - } - - /** - * Registers this instance as an autoloader. - * - * @param bool $prepend - */ - public function register($prepend = false) - { - spl_autoload_register(array($this, 'loadClass'), true, $prepend); - } - - /** - * Removes this instance from the registered autoloaders. - */ - public function unregister() - { - spl_autoload_unregister(array($this, 'loadClass')); - } -}