/** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output */ protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) { $jazzeeConfiguration = new \Jazzee\Configuration(); if ($jazzeeConfiguration->getStatus() == 'PRODUCTION') { $output->write('<error>You cannot scramble in production.</error>' . PHP_EOL); } else { if (!$jazzeeConfiguration->getAllowScramble()) { $output->write('<error>Scramble must be explicitly allowed by setting the allowScramble configuration setting.</error>' . PHP_EOL); } else { $start = time(); $entityManager = $this->getHelper('em')->getEntityManager(); $offset = 0; $maxResults = 100; $count = 0; $query = $entityManager->createQuery("SELECT a.firstName, a.lastName FROM \\Jazzee\\Entity\\Applicant a"); $query->setMaxResults(500); $firstNames = array(); $lastNames = array(); foreach ($query->getResult() as $arr) { $firstNames[] = $arr['firstName']; $lastNames[] = $arr['lastName']; } do { $query = $entityManager->createQuery("SELECT a FROM \\Jazzee\\Entity\\Applicant a"); $query->setMaxResults($maxResults); $query->setFirstResult($offset); $applicants = $query->getResult(); $continue = count($applicants); foreach ($applicants as $applicant) { $count++; $applicant->setFirstName($firstNames[array_rand($firstNames)]); $applicant->setLastName($lastNames[array_rand($lastNames)]); $applicant->setEmail("nobody{$count}@example.com"); $entityManager->persist($applicant); } $offset = $offset + $maxResults; $entityManager->flush(); $entityManager->clear(); gc_collect_cycles(); } while ($continue); $total = time() - $start; $avg = $count ? round($total / $count, 2) . 's/applicant' : ''; $usedMemory = round(memory_get_peak_usage() / 1048576, 2); $output->write("<info>{$count} applicant's information scrambled successfuly in {$total} seconds {$avg} using {$usedMemory}MB</info>" . PHP_EOL); } } }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output */ protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) { $jazzeeConfiguration = new \Jazzee\Configuration(); if ($jazzeeConfiguration->getStatus() == 'PRODUCTION') { $output->write('<error>You cannot create a demo in production.</error>' . PHP_EOL); exit; } $programName = $input->getOption('programName') ? $input->getOption('programName') : 'Demo'; $shortName = $input->getOption('shortName') ? $input->getOption('shortName') : 'demo'; $entityManager = $this->getHelper('em')->getEntityManager(); if ($entityManager->getRepository('\\Jazzee\\Entity\\Program')->findBy(array('name' => $programName))) { $output->write("<error>A program named '{$programName}' already exists.</error>" . PHP_EOL); exit; } if ($entityManager->getRepository('\\Jazzee\\Entity\\Program')->findBy(array('shortName' => $shortName))) { $output->write("<error>A program with the shortname '{$shortName}' already exists.</error>" . PHP_EOL); exit; } if (!($cycle = $entityManager->getRepository('\\Jazzee\\Entity\\Cycle')->findOneBy(array('name' => 'demo')))) { $cycle = new \Jazzee\Entity\Cycle(); $cycle->setName('demo'); $cycle->setStart('today'); $cycle->setEnd('next year'); $entityManager->persist($cycle); } $program = new \Jazzee\Entity\Program(); $program->setName($programName); $program->setShortName($shortName); $entityManager->persist($program); $application = new \Jazzee\Entity\Application(); $application->setProgram($program); $application->setCycle($cycle); $application->setWelcome('THIS IS A DEMO APP.'); $application->visible(); $application->setOpen('yesterday'); $application->setClose('next year'); $application->publish(true); $entityManager->persist($application); $StandardPageType = $entityManager->getRepository('\\Jazzee\\Entity\\PageType')->findOneBy(array('class' => '\\Jazzee\\Page\\Standard')); $page = new \Jazzee\Entity\Page(); $page->setTitle('Optional Page'); $page->optional(); $page->setType($StandardPageType); $entityManager->persist($page); $count = 1; foreach ($entityManager->getRepository('\\Jazzee\\Entity\\ElementType')->findAll() as $type) { $element = $this->demoElement($type, $entityManager); $element->setTitle($type->getName()); $element->optional(); $element->setWeight($count); $page->addElement($element); $entityManager->persist($element); $count++; } $applicationPage = new \Jazzee\Entity\ApplicationPage(); $applicationPage->setApplication($application); $applicationPage->setPage($page); $applicationPage->setKind(\Jazzee\Entity\ApplicationPage::APPLICATION); $applicationPage->setWeight(1); $entityManager->persist($applicationPage); $page = new \Jazzee\Entity\Page(); $page->setTitle('Required Page'); $page->setType($StandardPageType); $entityManager->persist($page); $count = 1; foreach ($entityManager->getRepository('\\Jazzee\\Entity\\ElementType')->findAll() as $type) { $element = $this->demoElement($type, $entityManager); $element->setTitle($type->getName()); $element->required(); $element->setWeight($count); $page->addElement($element); $entityManager->persist($element); $count++; } $applicationPage = new \Jazzee\Entity\ApplicationPage(); $applicationPage->setApplication($application); $applicationPage->setPage($page); $applicationPage->setKind(\Jazzee\Entity\ApplicationPage::APPLICATION); $applicationPage->setWeight(2); $entityManager->persist($applicationPage); $entityManager->flush(); $output->write("<info>Demo program {$programName} created successfully.</info>" . PHP_EOL); }
<?php namespace Jazzee\Console; try { //If the composer autoloader hasn't been loaded then load it here //We do it this way in case Jazzee has been built as a composer app into another app if (!class_exists('Composer\\Autoload\\ClassLoader', false)) { require __DIR__ . '/../vendor/autoload.php'; } $jazzeeConfiguration = new \Jazzee\Configuration(); //setup doctrine $doctrineConfig = new \Doctrine\ORM\Configuration(); //We use different caching and proxy settings in Development status if ($jazzeeConfiguration->getStatus() == 'DEVELOPMENT') { $doctrineConfig->setAutoGenerateProxyClasses(true); $doctrineConfig->setProxyDir(__DIR__ . '/../var/tmp'); } else { $doctrineConfig->setAutoGenerateProxyClasses(false); $doctrineConfig->setProxyDir(__DIR__ . '/../src/Jazzee/Entity/Proxy'); } $cache = new \Doctrine\Common\Cache\ArrayCache(); $driver = $doctrineConfig->newDefaultAnnotationDriver(array(__DIR__ . "/../src/Jazzee/Entity")); $doctrineConfig->setMetadataDriverImpl($driver); $doctrineConfig->setProxyNamespace('Jazzee\\Entity\\Proxy'); $doctrineConfig->setMetadataCacheImpl($cache); $doctrineConfig->setQueryCacheImpl($cache); $connectionParams = array('dbname' => $jazzeeConfiguration->getDbName(), 'user' => $jazzeeConfiguration->getDbUser(), 'password' => $jazzeeConfiguration->getDbPassword(), 'host' => $jazzeeConfiguration->getDbHost(), 'port' => $jazzeeConfiguration->getDbPort(), 'driver' => $jazzeeConfiguration->getDbDriver()); $em = \Doctrine\ORM\EntityManager::create($connectionParams, $doctrineConfig); } catch (Exception $e) { print $e->getMessage() . PHP_EOL;
/** * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output */ protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) { $error = false; $jazzeeConfiguration = new \Jazzee\Configuration(); $entityManager = $this->getHelper('em')->getEntityManager(); $stub = new AdminStub(); $stub->em = $entityManager; $stub->config = $jazzeeConfiguration; $requiredExtensions = array('ldap', 'apc', 'curl', 'json', 'imagick', 'mbstring', 'xml', 'dom'); $missing = array(); foreach ($requiredExtensions as $re) { if (!extension_loaded($re)) { $missing[] = $re; } } if (count($missing)) { $error = true; foreach ($missing as $m) { $output->write("<error>The PHP {$m} extension is required and it is not installed.</error>" . PHP_EOL); } } //Check that the var direcotry is working $path = $jazzeeConfiguration->getVarPath() ? $jazzeeConfiguration->getVarPath() : __DIR__ . '/../../../var'; if (!($realPath = \realpath($path)) or !\is_dir($realPath)) { if ($realPath) { $path = $realPath; //nicer error message if the path exists } $error = true; $output->write("<error>{$path} is does not exist so we cannot use it as the 'var' directory</error>" . PHP_EOL); } $perms = \substr(\sprintf('%o', \fileperms($realPath)), -4); $owner = \fileowner($realPath); $group = \filegroup($realPath); if (function_exists('posix_getpwuid')) { $arr = posix_getpwuid($owner); $owner = $arr['name']; } if (function_exists('posix_getgrgid')) { $arr = posix_getgrgid($group); $group = $arr['name']; } $output->write("<comment>{$realPath} is owned by {$owner}:{$group} and has permissions {$perms}. Ensure your webserver can write to that directory</comment>" . PHP_EOL); //Check that the schema is correct $validator = new \Doctrine\ORM\Tools\SchemaValidator($entityManager); $errors = $validator->validateMapping(); if ($errors) { $error = true; foreach ($errors as $className => $errorMessages) { $output->write("<error>The entity-class '" . $className . "' mapping is invalid:</error>" . PHP_EOL); foreach ($errorMessages as $errorMessage) { $output->write('* ' . $errorMessage . PHP_EOL); } $output->write(PHP_EOL); } } if (!$validator->schemaInSyncWithMetadata()) { $error = true; $output->write('<error>The database schema is not correct. Run "setup update" to fix this.</error>' . PHP_EOL); } try { $entityManager->getConfiguration()->ensureProductionSettings(); $entityManager->getConnection()->connect(); } catch (\Exception $e) { $error = true; $output->writeln('<error>' . $e->getMessage() . '</error>'); } if ($jazzeeConfiguration->getStatus() == 'DEVELOPMENT') { $error = true; $output->write("<error>Jazzee is in developer mode.</error>" . PHP_EOL); } else { //Check that the cache is setup if (!extension_loaded('apc')) { $error = true; $output->write("<error>APC is required for caching, but it it not installed.</error>" . PHP_EOL); } $metadatas = $entityManager->getMetadataFactory()->getAllMetadata(); $path = $entityManager->getConfiguration()->getProxyDir(); if (!is_dir($path)) { $directory = dirname($path); if (\realpath($directory)) { $directory = \realpath($directory); } $base = basename($path); if (!\is_writable($directory)) { $error = true; $output->write("<error>We do not have permission to create directory {$base} in {$directory}</error>" . PHP_EOL); } else { mkdir($path, 0777, true); } } if (!$error) { $proxyDir = \realpath($path); if (!\is_writable($proxyDir)) { $error = true; $output->write("<error>We do not have permission to write to {$proxyDir}.</error>" . PHP_EOL); } else { $entityManager->getProxyFactory()->generateProxyClasses($metadatas, $proxyDir); } } if (!apc_clear_cache() || !apc_clear_cache('user') || !apc_clear_cache('opcode')) { $output->write("<error>Error Clearning APC Cache</error>" . PHP_EOL); } } try { //Test the Authentication service $class = $jazzeeConfiguration->getAdminAuthenticationClass(); $adminAuthentication = new $class($stub); if (!$adminAuthentication instanceof \Jazzee\Interfaces\AdminAuthentication) { $error = true; $output->write("<error>{$class} does not implement AdminAuthentication Interface.</error>" . PHP_EOL); } //Test the directory service $class = $jazzeeConfiguration->getAdminDirectoryClass(); $adminDirectory = new $class($stub); if (!$adminDirectory instanceof \Jazzee\Interfaces\AdminDirectory) { $error = true; $output->write("<error>{$class} does not implement AdminDirectory Interface.</error>" . PHP_EOL); } // This isn't available until we can limit the search returned Issue #90 // $attributes = array( // $jazzeeConfiguration->getLdapLastNameAttribute() => '*' // ); // $results = $adminDirectory->search($attributes); } catch (\Jazzee\Exception $e) { $error = true; $output->writeln('<error>' . $e->getMessage() . '</error>'); } if (!class_exists('HTMLPurifier')) { $error = true; $output->write("<error>HTML Purifier is required and it is not installed.</error>" . PHP_EOL); } if (!class_exists('\\Doctrine\\ORM\\Version')) { $error = true; $output->write("<error>Doctrine ORM is required and it is not installed.</error>" . PHP_EOL); } if (!class_exists('\\Monolog\\Logger')) { $error = true; $output->write("<error>Monolog is required and it is not installed.</error>" . PHP_EOL); } if ($error) { $output->write(PHP_EOL . "<error>Preflight Check Failed</error>" . PHP_EOL); } else { $output->write(PHP_EOL . "<info>Preflight Check Passed</info>" . PHP_EOL); } }
/** * Get the cache * We use a static method here so the cache is always available * * @return \Foundation\Cache */ public static function getCache() { if (!isset(self::$_cache)) { $config = new \Jazzee\Configuration(); $foundationConfig = new \Foundation\Configuration(); if ($config->getStatus() == 'DEVELOPMENT') { $foundationConfig->setCacheType('array'); } else { $foundationConfig->setCacheType('apc'); } //use the path as a namespace so multiple installs on the same system dont conflict self::$_cache = new \Foundation\Cache('JAZZEE-' . str_ireplace(array('/', ' '), '', __DIR__), $foundationConfig); } return self::$_cache; }