IAM Role Name considred as Class in Yii 1

試著忘記壹切 提交于 2020-02-23 10:13:39

问题


I have attached the policy to EC2 IAM role to access AWS services. After that i have have used below code in Yii 1 controller file:

ExampleController.php

class ExampleController extends Controller
{
    public function init()
    {
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);
        //converting S3 private data to array to read
        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}

Output

PHP warning
include(TestRole.php): failed to open stream: No such file or directory

Note: TestRole is IAM Role Name.

I have used same code in Single PHP file (Not ties with Yii1)

test.php

require_once 'protected/extensions/awsv3/vendor/autoload.php';
$config = array(
        'version' => 'latest',
        'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
    'Names' => array('host_name'),
    'WithDecryption' => true
]);
//converting S3 private data to array to read
$keys = $result->toArray();
var_dump($keys);
exit("Exit");

array(3) { ["Parameters"]=> array(1) { [0]=> array(3) { ["Name"]=> string(12) "host_name" ["Type"]=> string(6) "String" ["Value"]=> string(9) "localhost" } } ["InvalidParameters"]=> array(0) { } ["@metadata"]=> array(4) { ["statusCode"]=> int(200) ["effectiveUri"]=> string(35) "https://ssm.us-west-2.amazonaws.com" ["headers"]=> array(4) { ["x-amzn-requestid"]=> string(36) "d3fb85bc-da4e-494b-be4f-b31fe3814100" ["content-type"]=> string(26) "application/x-amz-json-1.1" ["content-length"]=> string(3) "182" ["date"]=> string(29) "Tue, 19 Jun 2018 12:28:50 GMT" } ["transferStats"]=> array(1) { ["http"]=> array(1) { [0]=> array(0) { } } } } } Exit

its working with single php file.

So question is how to fix it in Yii 1 and why its considering IAM Role Name as Class file?

Stack Trace


回答1:


I was able to fix this out, Thanks to @javierfdezg.

So basically, Yii's auto loader and AWS's auto loader was got conflicted and may be due to Yii's assumption that class names must match file names.

So first i have unregistered the Yii's auto load then after the api call is finished registered it again.

class ExampleController extends Controller
{
    public function init()
    {
        /* Unregister  YiiBase */
        spl_autoload_unregister(array('YiiBase', 'autoload'));
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);

        /* Register  YiiBase */
        spl_autoload_register(array('YiiBase', 'autoload'));


        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}



回答2:


You may set Yii::$enableIncludePath to false - it should improve compatibility with other autoloaders.

Yii::$enableIncludePath = false;

After this Yii will stop blindly including class files assuming that they're in one of the directories specified in include_path. If class cannot be loaded by Yii autoloader, next autoloader will get his chance.



来源:https://stackoverflow.com/questions/50929974/iam-role-name-considred-as-class-in-yii-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!