/** * Create fake records (Admin in persons, Backend in modules) */ public static function generateFakeRecords() { $dbal = getApplication()->getStorage()->getAdapter(); $master = getApplication()->getStorage()->getMaster(); $admin = $master->getPersons()->create(array('hash' => '123', 'login' => 'admin', 'salt' => '123'))->save(); $backend = $master->getModules()->create(array('id_person_owner' => $admin->getIdPerson(), 'name' => 'Backend'))->save(); $user = $master->getPersons()->create(array('hash' => '123', 'login' => 'user', 'salt' => '321', 'id_module_default_module' => $backend->getIdModule()))->save(); $user->setSalt('123456'); $user->save(); /** * Move old models start time to the past */ $now = $dbal->fetchNow(); $time = strtotime($now); $veryPastTime = date("Y-m-d H:i:s", $time - 10000); $pastTime = date("Y-m-d H:i:s", $time - 5001); $startTime = date("Y-m-d H:i:s", $time - 5000); $dbal->executeQuery("update person set v_start = :very_past, v_end = :past where v_end < '9999-12-31 23:59:59'", array('very_past' => $veryPastTime, 'past' => $pastTime)); $dbal->executeQuery("update person set v_start = :start where v_end > :now", array('start' => $startTime, 'now' => $now)); /** * Clear repositories maps */ foreach (array($master->getModules(), $master->getPersons()) as $repo) { $mapProperty = Reflection::getReflectionProperty(get_class($repo), 'map'); $mapProperty->setAccessible(true); $mapProperty->setValue($repo, array()); $mapProperty->setAccessible(false); } }
private function getRepositoryMap($repository) { $mapProperty = Reflection::getReflectionProperty(get_class($repository), 'map'); $mapProperty->setAccessible(true); $map = $mapProperty->getValue($repository); $mapProperty->setAccessible(false); return $map; }
/** * @param string $nick * @param array $config * @return Behaviour * @throws \Exception */ public static function create(Model $model, $nick, $config = array()) { $class_name = sprintf('Cti\\Storage\\Behaviour\\%s', String::convertToCamelCase($nick)); if (!Reflection::getReflectionClass($class_name)->isSubclassOf(__CLASS__)) { throw new Exception(sprintf('Behaviour %s not found!', $nick)); } return new $class_name($model, $config); }
public function warm(Application $application) { parent::warm($application); $fs = new Filesystem(); $schema = $application->getStorage()->getSchema(); $entities = array(); foreach ($this->getClasses('Generator') as $class) { $reflection = Reflection::getReflectionClass($class); if (!$reflection->isAbstract() && $reflection->isSubclassOf('Cti\\Sencha\\Generator\\Generator')) { $entities[] = $reflection->getShortName(); } } foreach ($schema->getModels() as $model) { foreach ($entities as $entity) { if ($model->hasBehaviour('link') && $entity != 'Model' && $entity != 'Editor' || !$model->hasBehaviour('link') && $entity == 'Editor') { continue; } $generator = $this->application->getManager()->create('Cti\\Sencha\\Generator\\' . $entity, array('model' => $model, 'schema' => $schema)); $path = $this->application->getProject()->getPath('build coffee Generated ' . $entity . ' ' . $model->getClassName() . '.coffee'); $code = $generator->getGeneratedCode(); if (!file_exists($path) || md5(file_get_contents($path)) != md5($code)) { $fs->dumpFile($path, $code); } $path = $this->application->getProject()->getPath('build coffee ' . $entity . ' ' . $model->getClassName() . '.coffee'); $code = $generator->getFinalCode(); if (!file_exists($path) || md5(file_get_contents($path)) != md5($code)) { $fs->dumpFile($path, $code); } } echo '- generate ' . $model->getClassName() . PHP_EOL; } $generator = $this->application->getManager()->create('Cti\\Sencha\\Generator\\Master', array('model' => $model)); $path = $this->application->getProject()->getPath('build coffee Generated Master.coffee'); $code = $generator->getGeneratedCode(); if (!file_exists($path) || md5(file_get_contents($path)) != md5($code)) { $fs->dumpFile($path, $code); } $path = $this->application->getProject()->getPath('build coffee Master.coffee'); $code = $generator->getFinalCode(); if (!file_exists($path) || md5(file_get_contents($path)) != md5($code)) { $fs->dumpFile($path, $code); } $finder = new Finder(); $this->getCoffeeCompiler()->setDebug(true); $source = $application->getProject()->getPath('resources coffee'); if (is_dir($source)) { $finder->files()->name("*.coffee")->in($source); foreach ($finder as $file) { $script = substr($file, strlen($source) + 1, -7); echo '- processing ' . $script . '.coffee' . PHP_EOL; $this->getCoffeeCompiler()->build($script); } } }
/** * collect controllers list * @param Project $project * @throws \Cti\Core\Exception */ public function init(Project $project) { foreach ($project->getClasses('Controller') as $controller) { $location = '/'; $name = Reflection::getReflectionClass($controller)->getShortName(); $slug = String::camelCaseToUnderScore(substr($name, 0, -1 * strlen('Controller'))); if ($slug != 'default') { $location = '/' . $slug; } $this->add($location, $controller); } // validate base if ($this->base != '/') { if ($this->base[0] != '/') { throw new Exception('Base url not begins with /'); } elseif ($this->base[strlen($this->base) - 1] != '/') { throw new Exception('Base url not ends with /'); } if (strpos($_SERVER['REQUEST_URI'], $this->base) !== 0) { throw new Exception(sprintf('Base url (%s) is incorrect', $this->base)); } } // define method $this->method = strtolower($_SERVER['REQUEST_METHOD']); // define location $this->location = '/' . substr($_SERVER['REQUEST_URI'], strlen($this->base)); if ($this->location[strlen($this->location) - 1] != '/') { $this->location .= '/'; } // process controllers $controllers = $this->controllers; $this->controllers = array(); foreach ($controllers as $path => $controller) { if (is_numeric($path)) { $class = Reflection::getReflectionClass($controller)->getShortName(); if (substr($class, -10) == 'Controller') { $class = substr($class, 0, -10); $slug = String::camelCaseToUnderScore($class); if ($slug == 'default' || !$slug) { $path = '/'; } else { $path = '/' . $slug . '/'; } } } $this->add($path, $controller); } }
/** * @param $list */ function __construct($list) { $list = is_array($list) ? $list : func_get_args(); foreach ($list as $class) { $alias = basename(str_replace('\\', DIRECTORY_SEPARATOR, $class)); $this->classes[$alias] = $class; $this->actions[$alias] = array(); foreach (Reflection::getReflectionClass($class)->getMethods() as $method) { if (!$method->isConstructor()) { $len = 0; foreach ($method->getParameters() as $parameter) { if (is_null($parameter->getClass())) { $len++; } } $this->actions[$alias][] = array('name' => $method->getName(), 'len' => $len); } } } }
function process($instance, $parameters) { $class = get_class($instance); $inspector = $this->getInspector(); // injection contains class injection $injection = array(); foreach ($inspector->getClassInjection($class) as $name => $inject) { if ($inject['new']) { $injection[$name] = $this->getManager()->create($inject['class']); } else { $injection[$name] = $this->getManager()->get($inject['class']); } $list = array(); if ($this->references->offsetExists($injection[$name])) { $list = $this->references->offsetGet($injection[$name]); } $list[] = array('instance' => $instance, 'property' => $name); $this->references->offsetSet($injection[$name], $list); } $properties = $inspector->getClassProperties($class); foreach ($parameters as $name => $value) { if (isset($properties[$name])) { $injection[$name] = $value; } } foreach ($injection as $name => $value) { // public property if ($properties[$name]) { $instance->{$name} = $value; continue; } // protected property if ($this->getManager()->getConfigureAllProperties()) { $reflection = Reflection::getReflectionProperty($class, $name); $reflection->setAccessible(true); $reflection->setValue($instance, $value); $reflection->setAccessible(false); } } }
/** * warm application * @param Application $application * @return mixed */ public function warm(Application $application) { // define available classes $coreSource = dirname(__DIR__); $buildSource = $application->getProject()->getPath('build php'); $source = $application->getProject()->getPath('src php'); $path = array($coreSource, $source); if (is_dir($buildSource)) { $path[] = $buildSource; } $finder = new Finder(); $finder->in($path)->files(); $inspector = $application->getManager()->getInspector(); // warm inspector foreach ($finder as $file) { $path = $file->getPath(); if (strpos($path, $coreSource) === 0) { $namespace = 'Cti\\Core' . substr($path, strlen($coreSource)); } elseif (strpos($path, $buildSource) === 0) { $namespace = substr($path, strlen($buildSource) + 1); } elseif (strpos($path, $source) === 0) { $namespace = substr($path, strlen($source) + 1); } $class = str_replace(DIRECTORY_SEPARATOR, '\\', $namespace) . '\\' . $file->getBasename('.php'); if (!class_exists($class)) { continue; } $inspector->getPublicMethods($class); $inspector->getClassInjection($class); $inspector->getClassProperties($class); foreach (Reflection::getReflectionClass($class)->getMethods() as $method) { if ($method->getDeclaringClass()->getName() == $class) { $inspector->getMethodArguments($class, $method->getName()); $inspector->getMethodRequiredCount($class, $method->getName()); } } } $application->getCache()->set(__CLASS__, $this->get('Cti\\Di\\Cache')->getData()); }
/** * add class if it is console command * @param $class */ function processClass($class) { if (Reflection::getReflectionClass($class)->isSubclassOf('Symfony\\Component\\Console\\Command\\Command')) { $this->add($this->getManager()->get($class)); } }
public function getEntity() { return Reflection::getReflectionClass(get_class($this))->getShortName(); }
/** * sort warm by dependency * @param array $warm * @return array */ function processDependencies($warm) { $result = array_keys($warm); $dependencies = array(); foreach (array_keys($warm) as $index => $class) { if ($class == 'Cti\\Core\\Module\\Manager') { $last = $index; } else { $doc = Reflection::getReflectionClass($class)->getDocComment(); if ($doc) { foreach (explode(PHP_EOL, $doc) as $line) { if (stristr($line, '@dependsOn ')) { list($f, $dependency) = explode('@dependsOn ', $line); $dependencies[$class] = trim($dependency); } } } } } if (isset($last)) { $v = $result[$last]; unset($result[$last]); $result[] = $v; } $needSort = count($dependencies) > 0; $iterations = 0; while ($needSort) { if ($iterations++ > 10) { throw new Exception("Sort failed"); } $needSort = false; foreach ($dependencies as $class => $dependency) { $cIndex = array_search($class, $result); $dIndex = array_search($dependency, $result); if ($dIndex > $cIndex) { // swap keys $result[$dIndex] = $class; $result[$cIndex] = $dependency; $result = array_values($result); $needSort = true; break; } } } $aliasList = array(); foreach ($result as $class) { $aliasList[] = $warm[$class]; } return $aliasList; }