Skip to content

Commit

Permalink
Add daemonutils to locate autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbausor committed Mar 5, 2018
1 parent f714d67 commit f980c8d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
17 changes: 6 additions & 11 deletions Core/bin/google-cloud-batch
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@

namespace Google\Cloud\Core\Batch;

// Load the autoloader
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
// Called from local git clone.
require_once __DIR__ . '/../vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
// Called from google/cloud-core installation.
require_once __DIR__ . '/../../../autoload.php';
} elseif (file_exists(__DIR__ . '/../../../../autoload.php')) {
// Called from google/cloud installation.
require_once __DIR__ . '/../../../../autoload.php';
} else {
use Google\Cloud\Core\Daemon\DaemonUtils;

$autoloader = DaemonUtils::locateAutoloader(__DIR__ . '/..');
if (is_null($autoloader)) {
die('No autoloader found');
}

require_once $autoloader;

function showUsageAndDie()
{
die("usage: " . __FILE__ . " [daemon|retry] {id}");
Expand Down
46 changes: 46 additions & 0 deletions Core/src/Daemon/DaemonUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Core\Daemon;

/**
* Class DaemonUtils contains utility functions to support daemons
* @package Google\Cloud\Core\Daemon
*/
class DaemonUtils
{
private static $autoloaderCandidates = [
'/vendor/autoload.php', // Git clone of subcomponent
'/../vendor/autoload.php', // Git clone of google-cloud-php
'/../../autoload.php', // Subcomponent installation
'/../../../autoload.php', // google/cloud installation
];

/**
* @param string $subcomponentRoot Path to root of subcomponent for which to locate autoloader
* @return string Path to autoloader, or null
*/
public static function locateAutoloader($subcomponentRoot)
{
foreach (self::$autoloaderCandidates as $candidate) {
if (file_exists($subcomponentRoot . $candidate)) {
return $subcomponentRoot . $candidate;
}
}
return null;
}
}
17 changes: 6 additions & 11 deletions Debugger/bin/google-cloud-debugger
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@

namespace Google\Cloud\Debugger;

// Load the autoloader
if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
// Called from local git clone.
require_once __DIR__ . '/../../../vendor/autoload.php';
} elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
// Called from google/cloud-core installation.
require_once __DIR__ . '/../../../autoload.php';
} elseif (file_exists(__DIR__ . '/../../../../../autoload.php')) {
// Called from google/cloud installation.
require_once __DIR__ . '/../../../../../autoload.php';
} else {
use Google\Cloud\Core\Daemon\DaemonUtils;

$autoloader = DaemonUtils::locateAutoloader(__DIR__ . '/..');
if (is_null($autoloader)) {
die('No autoloader found');
}

require_once $autoloader;

function showUsageAndDie()
{
$filename = basename(__FILE__);
Expand Down

0 comments on commit f980c8d

Please sign in to comment.