Пример #1
0
        public static FieldTranslationInfo FromFieldInfo(FieldInfo fieldInfo, TranslationInfo info)
        {
            if (fieldInfo == null)
                return null;

            var fieldInfoDeclaringType = fieldInfo.DeclaringType;
            if (fieldInfoDeclaringType == null)
                throw new Exception("fieldInfo.DeclaringType is null"); // Resharper
            var fti = new FieldTranslationInfo();
            if (fieldInfo.IsLiteral)
                fti._destination = FieldTranslationDestionations.ClassConst;
            {
                fti._scriptName = fieldInfo.Name;
                var scriptNameAttribute = fieldInfo.GetCustomAttribute<ScriptNameAttribute>();
                if (scriptNameAttribute != null)
                {
                    fti._scriptName = scriptNameAttribute.Name;
                    fti._isScriptNamePhpEncoded = scriptNameAttribute.Kind == ScriptNameAttribute.Kinds.IntIndex;
                }
            }
            {
                var asDefinedConstAttribute = fieldInfo.GetCustomAttribute<AsDefinedConstAttribute>();
                if (asDefinedConstAttribute != null)
                {
                    fti._destination = FieldTranslationDestionations.DefinedConst;
                    if (!string.IsNullOrEmpty(asDefinedConstAttribute.DefinedConstName))
                        fti._scriptName = asDefinedConstAttribute.DefinedConstName;
                }
            }
            {
                var globalVariableAttribute = fieldInfo.GetCustomAttribute<GlobalVariableAttribute>();
                if (globalVariableAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination = FieldTranslationDestionations.GlobalVariable;
                    if (!string.IsNullOrEmpty(globalVariableAttribute.GlobalVariableName))
                        fti._scriptName = globalVariableAttribute.GlobalVariableName;
                }
            }
            {
                var asValueAttribute = fieldInfo.GetCustomAttribute<AsValueAttribute>();
                if (asValueAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti._destination = FieldTranslationDestionations.JustValue;
                    fti._usGlueForValue = asValueAttribute.Glue;
                }
            }
            var canBeNull = false;
            switch (fti._destination)
            {
                case FieldTranslationDestionations.JustValue:
                case FieldTranslationDestionations.GlobalVariable:
                    canBeNull = true;
                     fti._includeModule = null; // force null
                    break;
                case FieldTranslationDestionations.DefinedConst:
                case FieldTranslationDestionations.ClassConst:
                case FieldTranslationDestionations.NormalField:
                    var cti = info.GetOrMakeTranslationInfo(fieldInfoDeclaringType);
                    fti._includeModule = cti.ModuleName;
                    if (cti.BuildIn)
                    {
                        fti._includeModule = null;
                        canBeNull = true;
                    }
                    var isFieldOutsideClass = fti._destination == FieldTranslationDestionations.GlobalVariable ||
                                  fti._destination == FieldTranslationDestionations.DefinedConst;
                    {
                        // can be in other module for GlobalVariable and DefinedConst
                        var moduleAttribute = fieldInfo.GetCustomAttribute<ModuleAttribute>();
                        if (moduleAttribute != null)
                        {
                            if (!isFieldOutsideClass)
                                throw new Exception(string.Format("Module attribute can only be defined for GlobalVariable or DefinedConst. Check {0}.", fieldInfo.ExcName()));
                            fti._includeModule = new PhpCodeModuleName(moduleAttribute.ModuleShortName,
                                info.GetOrMakeTranslationInfo(fieldInfoDeclaringType.Assembly));
                        }
                    }
                    if (cti.IsPage)
                        fti._isDefinedInNonincludableModule = true;
                    if (!isFieldOutsideClass)
                    {
                        if (cti.IsArray || cti.Type.IsEnum || cti.BuildIn)
                        {
                            canBeNull = true;
                            fti._includeModule = null; // force null
                        }
                        else if (cti.DontIncludeModuleForClassMembers)
                            throw new Exception(
                                string.Format("field {0} belongs to nonincludable class (array, enum or skipped)",
                                    fieldInfo.ExcName()));
                    }
                    break;
            }

            if (!fti._includeModule.IsEmpty())
                return fti;
            if (canBeNull)
            {
                fti._includeModule = null; // can be not null but empty
                fti._isDefinedInNonincludableModule = false;
            }
            else
                throw new Exception(string.Format("Include module is empty for field {0}.",
                    fieldInfo.ExcName()));
            return fti;
        }
Пример #2
0
 private static void Check(FieldInfo fieldInfo, FieldTranslationInfo fti)
 {
     if (fti == null) throw new ArgumentNullException("fti");
     if (fti.Destination != FieldTranslationDestionations.NormalField &&
         fti.Destination != FieldTranslationDestionations.ClassConst)
         throw new Exception(string.Format("Unable to find right way to convert field {0}",
             fieldInfo.ExcName()));
 }